Versions Compared

Key

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

...

However, the output buffer is not flushed before receiving input back from the stream, so the data may not have actually been sent, resulting in unexpected behavior.

Compliant Solution

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

Code Block
bgColor#ccccff
/* some device used for both input and output */ 
char const *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);

This flush ensures that all data has been cleared from the buffer before continuing.

...