Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: check for fopen errors

...

Code Block
bgColor#ffcccc
/* 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 */
/* read response from file stream */
fclose(file);

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.

...

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 ensures that all data has been cleared from the buffer before continuing.

...