santosh said:
ericunfuk said:
Suppose fread() has failed because it encountered end of file and it
returned less items than it should have, now after this failure where
will the file position indicator be set to?Just at the end of the file?
The end-of-file indicator can be set if either all the characters have
been read or if there was an I/O error. To find out which it was, you
can use feof/ferror immediately after the call to fread. [...]
Not quite: The end-of-file indicator gets set when an
operation encounters the end of the file, and the error
indicator gets set when an operation encounters an error.
An I/O error does not set the end-of-file indicator (not
on its own account, anyhow; it's possible that some system
might encounter I/O errors during some kind of automatic
cleanup after end-of-file, so a single operation could set
both indicators -- but an I/O error, all by itself, doesn't
have anything to do with the end-of-file indicator).
Perhaps you're confusing the two indicators with the
single "it didn't work" value returned by an I/O function.
That value (EOF for some functions, NULL for others) declares
that something went wrong but doesn't say what: it could have
been end-of-file or it could have been an error (or perhaps
both). That's when you use feof() and ferror() to disambiguate.