...
In this example, an error flag is set upon fgets() failure. However, buf is not reset, and will have unknown contents.
| Code Block | ||
|---|---|---|
| ||
enum { BUFFERSIZE = 1024 }; char buf[1024BUFFERSIZE]; FILE *file; /* Initialize file */ if (fgets(buf, 1024sizeof(buf), file) == NULL) { /* set error flag and continue */ } printf("Read in: %s\n", buf); |
...
After fgets fails, buf is set to an error message.
| Code Block | ||
|---|---|---|
| ||
enum { BUFFERSIZE = 1024 }; char buf[1024BUFFERSIZE]; FILE *file; /* Initialize file */ if (fgets(buf, 1024sizeof(buf), file) == NULL) { /* set error flag and continue */ strcpy(buf, "fgets failed"); } printf("Read in: %s\n", buf); |
...