fread(3), unlike read(2) which returns a ssize_t, returns a
size_t. It doesn't distinguish between error and enf-of-file.
Instead, either ferror(3) or feof(3) need to be checked if fread()
returned 0.
Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
};
FILE *stream;
struct memfile_cookie mycookie;
- ssize_t nread;
+ size_t nread;
char buf[1000];
/* Set up the cookie before calling fopencookie() */
exit(EXIT_FAILURE);
}
nread = fread(buf, 1, 2, stream);
- if (nread == \-1) {
- perror("fread");
- exit(EXIT_FAILURE);
- }
if (nread == 0) {
+ if (ferror(stream) != 0) {
+ fprintf(stderr, "fread failed\en");
+ exit(EXIT_FAILURE);
+ }
printf("Reached end of file\en");
break;
}