我试图找到一种方法来选择文本文件的最后一行使用C(不是c ++或c#,只是C),我很难找到一种方法来做到这一点,如果有人可以帮助我解决这个问题我将非常感激,谢谢!(顺便说一下,我想要做的一个很好的例子,这将类似于尾巴-n 1将在bash中做什么)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(int argc, char *argv[])
{
FILE *fd; // File pointer
char filename[] = "./Makefile"; // file to read
char buff[1024];
if ((fd = fopen(filename, "r")) != NULL) // open file
{
fseek(fd, 0, SEEK_SET); // make sure start from 0
while(!feof(fd))
{
memset(buff, 0x00, 1024); // clean buffer
fscanf(fd, "%[^\n]\n", buff); // read file *prefer using fscanf
}
printf("Last Line :: %s\n", buff);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Linux.CMIIW
没有直接的方法,但我首选的方法是: