C: feof 的奇怪行为

C: strange behavior of feof

我是 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 

来自 fseek 文档:

The end-of-file internal indicator of the stream is cleared after a successful call to this function, and all effects from previous calls to ungetc on this stream are dropped.