feof()
does not detect if the file has ended. It detects if the last read error was due to the file having ended.
feof()
only happens after a failed read.
So, first read data and check the return value. If the read failed use feof()
to make sure it failed because the END-OF-FILE was reached (other reasons for the read to fail are error of some kind (network down, bad sector, printer on fire, ...), detectable with ferror()
).