Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: deleted some extraneous code to alleviate confusion and put emphasis on the acutal problem

...

In this non-compliant code example, a device is opened for updating, data are sent to it, and then the response is read back.

Code Block
bgColor#ffcccc
/* location to be read or written to */
const char *filename = "/dev/device2";

FILE *file = fopen(filename, "rb+");
if (file == NULL) {
  /* handle error */
}

/* write to file stream */
/* read response from file stream */
fclose(file);

...

In this compliant solution, fflush() is called in between the output and input.

Code Block
bgColor#ccccff

/*location to be read or written to */
const char *filename = "/dev/device2";

FILE *file = fopen(filename, "rb+");
if (file == NULL) {
  /* handle error */
]

/* write to file stream */
fflush(file);
/* read response from file stream */
fclose(file);

...