3

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(fp))
    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?

sud03r
  • 19,109
  • 16
  • 77
  • 96

5 Answers5

6

If you're reading a byte at a time, the idiomatic way to do this is with fgetc:

int c;
while ((c = fgetc(fp)) != EOF) {
   // Do something.
}

and then you shouldn't need to deal with feof.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • 2
    You still need to deal with feof (or ferror). When the loop terminates, you need to check if it terminated because it reached the end of the file, or if there was a read error. The only way to do that is with one of those functions. (Unless you don't care about reporting errors.) – William Pursell Mar 26 '11 at 12:59
3

I typically do something like this:

int get_next_char(FILE* fp, char *ch)
{
    return fread(ch, sizeof(char),1, fp) == 1;
}

// main loop
char ch;
while (get_next_char(fp, &ch))
    process_char(ch);

if (!feof(fp))
    handle_unexpected_input_error(fp);
Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
  • I think this is the best advice - refactor you calling code so it doesn't need to check for eof before trying to read (which effectively will mean buffering one character ahead I suppose). – caf Apr 13 '10 at 23:42
2

It's best to structure your code so that you try to read some data and if the read does not succeed due to reaching the end-of-file, you deal with it there (i.e. see Kristopher Johnson's answer).

If you absolutely hate this, you can use ungetc to return a single character back to the stream and it will be available in the next read call:

int c = fgetc(fp);
if (c == EOF)
{
    // handle eof / error
}
else
{
    ungetc(c, fp);

    // the next read call is guaranteed to return at least one byte

}
Community
  • 1
  • 1
R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
0

I'm not clear, but if you're interested in knowing before you read the byte if EOF has been reached, put your feof() test before the fread() rather than after it.
actually, if I read correctly, you don't even want to do the fread so:

return feof(fp) ? true : false;
KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
  • 1
    That's not how `feof` works. Just being at the end of the file is insufficient; you must try to read **past** the end of the file before `feof` returns true. – R Samuel Klatchko Apr 14 '10 at 00:11
  • correct. that doesn't make my answer wrong, just irrelevant (the OP should have already recv'd an error on a read past eof) – KevinDTimm Apr 14 '10 at 14:12
0

I would suggest using:

fseek(myFile, 0, SEEK_END);
fileLen = ftell(myFile);
fseek(myfile, 0, SEEK_SET);

When you first open the file to determine the length. Then set up your read-loop to never read off the end. You can use ftell to figure out where you are, and compare to fileLen to figure out how much further you have to go.

abelenky
  • 63,815
  • 23
  • 109
  • 159