我正在从一个使用以下方式打开的文件中读取数据到内存中:
FILE *f = fopen(path, "rb");
在开始从文件中复制字节之前,我使用以下方法将指针定位到起始位置:
/**
* Goes to the given position of the given file.
*
* - Returns 0 on success
* - Returns -1 on EOF
* - Returns -2 if an error occured, see errno for error code
* - Returns -3 if none of the above applies. This should never happen!
*/
static int8_t goto_pos(FILE *f, uint64_t pos)
{
int err = fseek(f, pos, SEEK_SET);
if (err != 0) {
if (feof(f) != 0) return -1;
if (ferror(f) != 0) return -2;
return -3;
}
return 0;
}
问题是,尽管我寻求的位置远远超过了EOF
,但此函数从未返回-1。
根据参考文献,当遇到EOF
时,feof
应该返回非零值。
为什么会这样?feof
函数无用吗?
请注意,我目前正在使用fgetc
的返回值来检查EOF
。