我是C的新手。我偶然发现了feof
我无法解释的某些行为。具体在下面的代码中我创建一个文件,在其中写入一个字节的信息,然后关闭并再次打开它,读取信息(我的1个字节)直到达到EOF
,然后移动文件的当前位置指针0
字节(即根本不改变当前位置)突然间我不再处于EOF
。怎么会?
#include <stdio.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(void) {
FILE* f = fopen("myfile.txt","w");
BYTE b = 0x0000;
fwrite(&b,1,1,f);
fclose(f);
f = fopen("myfile.txt","r");
while (!feof(f)){
fread(&b,1,1,f);
}
printf("We have reached EOF: %i \n",feof(f));
fseek(f,0,SEEK_CUR);
printf("We have reached EOF: %i \n",feof(f));
}
输出
We have reached EOF: 1
We have reached EOF: 0