Mail Archives: djgpp/1997/07/19/02:17:50
A. Sinan Unur wrote:
> that is at best worthless advice. the documentation in this case is
> plain wrong. it is a small mistake, but fread is supposed to return 0
> in case of error.
oh, boy, do i love correcting myself. i checked the excerpt from the
ansi standard in Plauger's "The Standard C Library". it says:
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 may be less than nmemb if a read error or EOF is encountered. If
size or nmemb is zero, fread returns zero and the contents of the array
and the state of the stream remain unchanged.
Later on, on p.271, he comments: "if the second argument is greater than
one, you cannote 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*n, stream)
instead of
fread(buf, size, n, stream)."
it follows that you can only count on fread returning zero in case of
failure if you use the latter form.
--
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 -