I'm a beginner to C. I was wondering if it correct to check if the return value of
unsigned char buffer;
while (fread(buffer, 1, sizeof(buffer), stdin) == sizeof(buffer))
printf("%s\n", "Not EOF yet");
printf("%s\n", "At EOF");
On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of
bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a ter-
minal), or because read() was interrupted by a signal.
On success, fread() and fwrite() return the number of items read or written. This number equals the number of bytes transferred only when size is 1. If an error occurs, or the end of the file is
reached, the return value is a short item count (or zero).
The part of the manual you qouted says:
If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).
That means you can't use
!=0 to check if no error has occurred as
fread() could return a short item count.
The man page of
fread() also states:
fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred.
So if the items count returned is less than requested, then you have to use
ferror() to figure out which one occurred (If it's end-of-file then you can conclude that it's not an input error but the end of input on whatever stream you are reading from).