Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: close/fclose error checks

...

Code Block
bgColor#ffcccc
langc
#include <stdio.h>

void open_some_file(const char *file) {
  FILE *f = fopen(file, "r");
  if (NULL != f) {
    /* File exists, handle error */
  } else {
    if (fclose(f) !== 0EOF) {
      /* Handle error */
    }
    f = fopen(file, "w");
    if (NULL == f) {
      /* Handle error */
    }
 
    /* Write to file */
    if (fclose(f) !== 0EOF) {
      /* Handle error */
    }
  }
}

...

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

void open_some_file(const char *file) {
  FILE *f = fopen(file, "wx")
  if (NULL == f) {
    /* Handle error */
  }
  /* Write to file */
  if (fclose(f) !== 0EOF) {
    /* Handle error */
  }
}

...

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

void open_some_file(const char *file) {
  int fd = open(file, O_CREAT | O_EXCL | O_WRONLY);
  if (-1 != fd) {
    FILE *f = fdopen(fd, "w");
    if (NULL != f) {

      /* Write to file */

       if (fclose(f) !== 0EOF) {
        /* Handle error */
      }
    }
    if (close(fd) !== 0-1) {
      /* Handle error */
    }
  }
}

...

Code Block
bgColor#ffcccc
langc
#include <stdio.h>
#include <unistd.h> 

void open_some_file(const char *file) {
  if (access(file, R_OK) == 0) {
    FILE *f = fopen(file, "rb");
    if (NULL == f) {
      /* Handle error */
    }
    /* Read file */
    if (fclose(f) !== 0EOF) {
      /* Handle error */
    }
  }
}

...

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

void open_some_file(const char *file) {
  FILE *f = fopen(file, "rb");
  if (NULL == f) {
    /* Handle error */
  }
  /* Read file */
  if (fclose(f) !== 0EOF) {
    /* Handle error */
  }
}

...