Versions Compared

Key

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

...

When a file is opened with update mode ('+' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file. Opening (or creating) a text file with update mode may instead open (or create) a binary stream in some implementations.

...

Noncompliant Code Example

The following This noncompliant code example appends data to a file and then reads from the same file.

Code Block
bgColor#ffcccc
char data[BUFSIZ];
char append_data[BUFSIZ];
char *file_name;
FILE *file;

/* initializeInitialize file_name */

file = fopen(file_name, "a+");
if (file == NULL) {
  /* handleHandle error */
}

/* initialize append_data */

if (fwrite(append_data, BUFSIZ, 1, file) != BUFSIZ) {
  /* handleHandle error */
}
if (fread(data, BUFSIZ, 1, file) != 0) {
  /* handleHandle there not being data */
}

fclose(file);

...

Code Block
bgColor#ccccff
char data[BUFSIZ];
char append_data[BUFSIZ];
char *file_name;
FILE *file;

/* initialize file_name */

file = fopen(file_name, "a+");
if (file == NULL) {
  /* handleHandle error */
}

/* initializeInitialize append_data */

if (fwrite(append_data, BUFSIZ, 1, file) != BUFSIZ) {
  /* Handle error */
}

if (fseek(file, 0L, SEEK_SET) != 0) {
  /* Handle Errorerror */
}

if (fread(data, BUFSIZ, 1, file) != 0) {
  /* handleHandle there not being data */
}

fclose(file);

...