FSEEK上的位置超出EOF不使用的feof触发EOF,怎么来的? [英] fseek on a position beyond EOF does not trigger EOF using feof, how come?

查看:430
本文介绍了FSEEK上的位置超出EOF不使用的feof触发EOF,怎么来的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从一个文件到内存在打开与读取数据

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

的问题是,即使我试图超越 EOF 的位置的方式,这个功能永远不会返回-1。

The problem is that even though I seek to a position way beyond EOF, this function never returns -1.

根据文献的feof 应返回时遇到 EOF 非零值。

According to the reference feof should return a non zero value when EOF is encountered.

这是为什么?是的feof 功能没用?

Why is this? Is the feof function useless?

请注意,我目前使用龟etc 的返回值来检查 EOF

Note that I'm currently using the return value of fgetc to check for EOF.

推荐答案

寻求根本不测试文件的末尾。

Seeking simply does not test for the file's end.

这样做的原因是,你也许可能想要做一个的fwrite(),你寻求。 fseek的()无法知道你的计划是什么,它​​被称为后。

The reason for this is that you perhaps might want to do an fwrite() where you sought to. fseek() can not know what your plans are for after it was called.

做一个 FREAD()寻找文件的末尾后面,你将不得不在的feof()返回一个非 - 零值。

Do an fread() after seeking behind the file's end and you will have feof() returning a non-zero value.

这篇关于FSEEK上的位置超出EOF不使用的feof触发EOF,怎么来的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆