Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

c - 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.

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

Why is this? Is the feof function useless?


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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. fseek() can not know what your plans are for after it was called.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...