我在使用这个可以读取文件的小功能时遇到了一些问题:
void ReadFile(char *name) {
FILE *fr;
int lenght, i;
fr = fopen(name, "r"); //Open the file reader
fseek(fr, 0, 2); //Set the pointer at the EOF
lenght = ftell(fr); //Read the ending position
printf("\nDEBUG lenght:%d\n", lenght);
fseek(fr, 0, 0); //Return at the beginning of the file
printf("File read:\n\n");
for (i = 1; i <= lenght; i++) {
printf("%c", getc(fr));
fseek(fr, i, 0);
}
fclose(fr);
}
这是它读取的文件:
qwerty
asdfgh
zxcvbn
但这是程序的输出:
DEBUG lenght:24
File read:
qwerty
asdfgh
zxcvbn
当之前有一个时,它基本上是读取一个额外的“\ n”。
关于为什么代码不起作用的任何想法?
谢谢