Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In this example, an error flag is set upon fgets() failure. However, buf is not reset, and will have unknown contents.

Code Block
bgColor#ffcccc
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
bgColor#ccccff
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);

...