fseek在EOF之外的位置不会触发使用feof的EOF,为什么会这样?

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()返回非零值.

  • 不仅要求不测试eof; 它清除了eof标志. (8认同)

Dav*_*eri 5

feof()在尝试读取失败之后,fgets()fread()之前或之前设置.


小智 5

寻求超出 EOF 将在后续写入后扩大文件,因此这是一致的,与其他人的看法相反。见这里: fseek

fseek() 函数应允许将文件位置指示符设置为超出文件中现有数据的末尾。如果稍后写入数据,则后续读取间隙中的数据将返回值为 0 的字节,直到数据实际写入间隙。

这就是 fseek 不能返回 EOF 的原因。如果您尝试从之前未向该位置或后面写入任何内容的位置读取,您稍后将获得 EOF。所以这都是正确的行为。