rze*_*erg 10 c linux io fseek eof
我正在从一个文件读取数据到打开的内存:
FILE *f = fopen(path, "rb");
Run Code Online (Sandbox Code Playgroud)
在我开始从文件中复制字节之前,我使用以下方法寻找起始位置:
/**
* 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;
}
Run Code Online (Sandbox Code Playgroud)
问题在于,即使我寻求超越位置EOF,此函数也不会返回-1.
根据引用feof,EOF遇到时应返回非零值.
为什么是这样?这个feof功能没用吗?
请注意,我目前正在使用fgetc要检查的返回值EOF.
alk*_*alk 12
寻求根本不测试文件的结束.
这样做的原因是你可能想要做一个fwrite()你想要的地方.fseek()在被召唤之后无法知道你的计划是什么.
fread()在文件结束后执行后,您将feof()返回非零值.