feof() in C file handling
        Posted  
        
            by Neeraj
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Neeraj
        
        
        
        Published on 2010-04-13T23:20:35Z
        Indexed on 
            2010/04/13
            23:23 UTC
        
        
        Read the original article
        Hit count: 466
        
I am reading a binary file byte-by-byte,i need determine that whether or not eof has reached.
feof() doesn't works as "eof is set only when a read request for non-existent byte is made". So, I can have my custom check_eof like:
if ( fread(&byte,sizeof(byte),1,fp) != 1) {
    if(feof())
    return true;
}
return false;
But the problem is, in case when eof is not reached, my file pointer is moved a byte ahead.
So a solution might be to use ftell() and then fseek() to get it to correct position.
Another solution might be to buffer the byte ahead in some temporary storage.
Any better solutions?
© Stack Overflow or respective owner