简体   繁体   English

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

[英]fseek on a position beyond EOF does not trigger EOF using feof, how come?

I'm reading data from a file to memory that is opened with: 我正在从一个文件读取数据到打开的内存:

FILE *f = fopen(path, "rb");

Before I start copying bytes from the file I seek to a start position using: 在我开始从文件中复制字节之前,我使用以下方法寻找起始位置:

/**                                                                                                                                                    
 * 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;                                                                                                                                      
}

The problem is that even though I seek to a position way beyond EOF , this function never returns -1. 问题在于,即使我寻求超出EOF的位置,此函数也不会返回-1。

According to the reference feof should return a non zero value when EOF is encountered. 根据参考,当遇到EOF时, feof应返回非零值。

Why is this? 为什么是这样? Is the feof function useless? feof功能无用吗?


Note that I'm currently using the return value of fgetc to check for EOF . 请注意,我目前正在使用fgetc的返回值来检查EOF

Seeking simply does not test for the file's end. 寻求根本不测试文件的结束。

The reason for this is that you perhaps might want to do an fwrite() where you sought to. 这样做的原因是你可能想要在你想要的地方做一个fwrite() fseek() can not know what your plans are for after it was called. fseek()在调用之后无法知道你的计划是什么。

Do an fread() after seeking behind the file's end and you will have feof() returning a non-zero value. 在文件结束后搜索后执行fread() ,你将得到feof()返回非零值。

feof()在尝试读取失败后设置,所以fgets()fread()之前。

Seeking beyond the EOF will enlarge the file after a subsequent write, so this is conformant, contrary to what others believe. 在EOF之后寻求将在后续写入之后放大文件,因此这符合其他人的信念。 See here: fseek 见这里: fseek

The fseek() function shall allow the file-position indicator to be set beyond the end of existing data in the file. fseek()函数应允许将文件位置指示符设置为超出文件中现有数据的末尾。 If data is later written at this point, subsequent reads of data in the gap shall return bytes with the value 0 until data is actually written into the gap. 如果此时稍后写入数据,则后续读取间隙中的数据将返回值为0的字节,直到数据实际写入间隙。

That's why fseek cannot return EOF. 这就是fseek无法返回EOF的原因。 You will later get an EOF if you try to read from a position when nothing had been previously written to that position or behind. 如果您尝试从以前没有任何内容写入该位置或后面的位置读取,您将稍后获得EOF。 So this is all correct behavior. 所以这是正确的行为。

Seeking beyond the end of the file on some operating systems increases the size of the file. 在某些操作系统上查找超出文件末尾会增加文件的大小。 As long as there's space to make the file larger, it will never return an error. 只要有足够的空间使文件变大,它就永远不会返回错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM