Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdio.h

C fclose() function

Reference for C fclose(): flush buffered output, release the file handle, check the return value for write errors and avoid use-after-close bugs.

fclose() ends your connection to a file. It writes any data still waiting in the stream's buffer, closes the underlying file, and releases the FILE object. After the call, the pointer is invalid: you must not use it again, not even to call fclose() a second time.

Closing is also where a write can finally fail. Output you wrote with fprintf() or fwrite() often sits in a buffer, and a full disk or a network drive error may only show up when that buffer is flushed. That is why a careful program checks the return value of fclose() on files it wrote.

C
#include <stdio.h>

int fclose(FILE *stream);

Try it

C Close, then check the size on disk

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C A helper that always closes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
int fclose(FILE *stream);
Name Description
stream A stream returned by fopen(), tmpfile(), freopen() or fdopen() that is still open.

Returns: 0 on success, EOF if flushing or closing failed (check errno). Even when it returns EOF, the stream is closed afterwards; you cannot retry.

Errors / undefined behavior: using stream after fclose() (reading, writing, or closing again) is undefined behavior. Passing NULL is undefined behavior, so guard it when a pointer may be unset. Closing stdin, stdout or stderr is allowed but rarely wise.

Usage notes

  • Always close what you open. Each open stream holds a file descriptor and a buffer. A process has a limited number of descriptors, so a loop that opens files without closing them eventually fails with EMFILE.
  • Check the result after writing. if (fclose(out) != 0) catches errors that only appear at flush time. Errors from earlier writes also surface here, because the stream's error flag makes fclose() fail. For read-only streams the check is optional.
  • Closing flushes. You do not need a separate fflush() before fclose(). Another program reading the file will see everything after it returns.
  • Normal exit closes for you. Returning from main() or calling exit() flushes and closes all open streams. abort(), a crash, or _exit() does not, and buffered data is lost.
  • Set pointers to NULL after closing if they live longer than the function (fclose(f); f = NULL;). It turns a use-after-close crash into a clearer null check.
  • Early returns leak. Every return between fopen() and fclose() must close the file first. Funnel failures through one cleanup label with goto, or keep the file in a small helper function as in the second example.
  • Do not copy a FILE object. Always pass the pointer; the structure is opaque.
  • Renaming and removing. On Windows you cannot remove or rename an open file, so close first. On POSIX you can, but close first anyway for clarity.
  • Complexity: the cost is mostly the flush of buffered data.