c fclose()


fclose() Function

To close a file you need to use the fclose function, fclose returns zero if the file is closed successfully.

  int fclose(FILE *a_file);

fopen modes

  • r - open for reading.
  • w - open for writing (file need not exist).
  • a - open for appending (file need not exist).
  • r+ - open for reading and writing, start at beginning.
  • w+ - open for reading and writing (overwrite file).
  • a+ - open for reading and writing (append if file exists).

Note that it's possible for fopen to fail even if your program is perfectly correct: you might try to open a file specified by the user, and that file might not exist (or it might be write-protected). In those cases, fopen will return 0, the NULL pointer.

Example of using fclose: