I have a while (!feof(infile)) loop, so that exits only when the eof is reached. And it closes all files upon exit. Here's the code:

// scan input file
while (!feof(infile))
    // read four bytes
    BYTE byte[4];
    fread(&byte, sizeof(BYTE), 4, infile);
  • 1
    I put through a suggested edit to remove most of the code, due to the Honor Code. Thanks.
    – curiouskiwi
    Jun 11, 2014 at 8:47
  • Sorry, I forgot about that. Thanks. Jun 11, 2014 at 12:44

1 Answer 1


feof is only set after fread tries to read the file and fails. When you reach the end of the file, fread will run one more time and your loop will carry on and write one more block (the last one, again). This will cause your last file to have an extra block in it, and fail check50.

Try to think of another way to know you have reached the end of the file.

  • 1
    I added another later of error checking! Now, inside the while loop, before reading the file, it checks if the eof has been reached. Works like a charm! Thanks! Jun 11, 2014 at 13:16
  • THANK YOU!! Works for me as well :-)
    – julio
    Oct 1, 2014 at 1:41
  • Phew. Thank you too! Dec 18, 2014 at 21:09
  • Super helpful! Thanks! Jun 30, 2015 at 14:07
  • I think I have the same problem, can you please elaborate? Jul 2, 2015 at 9:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .