mirror of https://github.com/mkerrisk/man-pages
fopencookie.3: Fix bugs in example
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>
This commit is contained in:
parent
a1eb1a6a33
commit
a3eeca342d
|
@ -392,7 +392,7 @@ main(int argc, char *argv[])
|
|||
};
|
||||
FILE *stream;
|
||||
struct memfile_cookie mycookie;
|
||||
ssize_t nread;
|
||||
size_t nread;
|
||||
char buf[1000];
|
||||
|
||||
/* Set up the cookie before calling fopencookie() */
|
||||
|
@ -429,11 +429,11 @@ main(int argc, char *argv[])
|
|||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue