fclose · shipping · also fclose(), fclose(3)

fclose

fclose is a C standard-I/O operation that flushes pending output and closes a stream, releasing its underlying file descriptor when one is present.

On Linux, check the return: zero indicates success and EOF reports failure with errno set, potentially from writing, flushing or closing. Earlier buffered output can appear successful before fclose reports an error. Neither successful fclose nor successful fflush adds fsync semantics. [1]

After fclose returns, including failure, the FILE pointer must not be used again; retrying fclose on it has undefined behavior. This stream-lifetime rule is distinct from the platform-specific close(2) descriptor rules. Do not confuse a retired stream with an open file descriptor. [1]

Relations

related
fflush, fwrite, close, fsync, File descriptor

Limitations

  • Stream closure is not proof of persistent file data.
  • A failed fclose cannot be retried using the same FILE pointer.

Citations

  1. Linux man-pages 6.19: fclose(3) (accessed 2026-10-21)

Further reading

  1. Linux man-pages 6.19: fflush(3) (accessed 2026-10-21)
  2. Hesela: The user-space buffer comes before fsync (accessed 2026-10-21)

JSON: /data/terms/fclose.json

Published by Hesela team · Updated

Report a correction · Editorial standards · Reuse guidance

Cite this page
Hesela team. fclose. Hesela. 2026-10-21. https://hesela.dev/terms/fclose/