因此,使用 fseek 和 ftell 函数,我尝试一次打印一个字符,方法是首先使用 fseek 指向文件的末尾,然后递减 ftell 的值以打印最后一个字符,然后打印前一个字符。
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp=NULL;
char i=0,count=0;
fp= fopen("note.txt","r");
if(fp==NULL){ printf("File error");
return -1;}
fseek(fp,0,SEEK_END);
count=ftell(fp);
while(count){
count--;
fseek(fp,count,SEEK_END);
i=fgetc(fp);
printf("%c",i);
}
fclose(fp);
fp=NULL;
return 0;
}
我的代码正在打印一些奇怪的字符,所以它不起作用,关于我在 while 循环中哪里出错的任何建议?