0
我试图编写一个程序来读取文件,并输出文件的行。它将从最后一行开始,然后打印第二行到最后一行,然后是最后一行,第二行到最后一行,然后是第三行到最后一行,依此类推。 ((c = fgetc(myFile)!= EOF))while((c = fgetc(myFile))!= EOF) 这是循环的条件, 代码(c = fgetc ....)关闭。
有人可以帮我解决这个问题吗?
谢谢。学习如何读取和输出文件中的行C
void tail(FILE* myFile, int num) //Tail function that prints the lines
according to the user specified number of lines
{
int start, line = 0, counter = 0;
char c, array[100];
while((c = fgetc(myFile) != EOF))
{
if(c=='\n')
line++;
}
start = line - num; //Start location
fseek(myFile, 0, SEEK_SET); //Goes to the start of the file
while(fgets(array, 100, myFile) != NULL)
{
if(counter >start)
{
printf("%s",array); //Prints the string
}
counter++;
}
fclose(myFile); //Closes the file
}