Mail Archives: djgpp/1997/07/19/20:46:51
Nate Eldredge wrote:
>
> A. Sinan Unur wrote:
> >heretic AT polbox DOT com wrote:
> >> So, I wanted to have error checking and I wrote something like
> >>
> >> if ( fread(blah,blah,blah) == -1 )
> >> {
> >> oh_no_error ();
> >> }
> >>
> >
> >i think it is an error in the docs. AFAIK, fread is supposed to
> >return 0, not EOF, precisely because the return value is size_t, and
> >signifies no bytes read.
>
> Well, AFAIK, fread is an ANSI function, so the standard should be able
> to bring enlightenment. Can someone with a copy help us out here?
at the time, i did not have access to any books. anyway, later on i
checked plaugers "the standard C library. the following is an excerpt
from the standard (taken from p248 of that book):
7.9.8.1 The fread function
Synopsis
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
...
Returns
The fread function returns the number of elements successfully read,
which maybe less than nmemb if a read error or EOF is encountered. ...
later on, on p271, plauger remarks:
" ... if the size (second) argument is greater than one, you cannot
determine whether the function also read up to size-1 additional
characters beyond what it reports. As a rule, you are better off calling
the function as fread(buf, 1, size*nmemb, stream) instead of
fread(buf, size, nmemb, stream)."
i hope this clarifies the confusion i helped create.
the point remains that the docs are wrong in stating that it returns -1
on error. however, it is an even bigger mistake (as was suggested by
someone else during this thread) to suppress the warning that gcc emits
and essentially comparing the return value to (size_t) -1.
i think how to check the return value has already been clarified by art
kagel, but thought i should re-post the quote from the standard.
--
Sinan
*******************************************************************
A. Sinan Unur WWWWWW
|--O+O
mailto:sinan DOT unur AT cornell DOT edu C ^
http://www.people.cornell.edu/pages/asu1/ \ ~/
Unsolicited e-mail is _not_ welcome, and will be billed for.
*******************************************************************
- Raw text -