Versions Compared

Key

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

...

Calling abort() causes abnormal program termination to occur unless the SIGABRT signal is caught and the signal handler calls exit() or _Exit().:

Code Block
bgColor#ccccff
langc
#include <stdlib.h>
/* ... */

if (/* something really bad happened */) {
  abort();
}

...

In this compliant solution, the call to abort() is replaced with exit(), which guarantees that buffered I/O data is flushed to the file descriptor and the file descriptor is properly closed.:

Code Block
bgColor#ccccff
langc
#include <stdlib.h>
#include <stdio.h>

int write_data(void) {
  const char *filename = "hello.txt";
  FILE *f = fopen(filename, "w");
  if (f == NULL) {
    /* Handle error */
  }
  fprintf(f, "Hello, World\n");
  /* ... */
  exit(EXIT_FAILURE); /* Writes data and closes f. */
  /* ... */
  return 0;
}

int main(void) {
  write_data();
  return EXIT_SUCCESS;
}

...