fclose(3)

โœจ fclose(3) โ€” Library Functions Manual โœจ

okay bestie, gather round, because today we're closing a stream and it's honestly iconic ๐Ÿ’–


๐Ÿ’ซ NAME ๐Ÿ’ซ

fclose โ€” close a stream

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc) โ€” she's literally built in, no extra linking required, we love a low-maintenance queen ๐Ÿ‘‘

๐ŸŽ€ SYNOPSIS ๐ŸŽ€

#include <stdio.h>

int fclose(FILE *stream);

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

So the way this works is: fclose() flushes the stream that stream points to โ€” like, it writes out any buffered output data that's still chilling in the buffer, using fflush(3) for the actual flushing โ€” and then it closes the underlying file descriptor.

That's it. That's the whole job. Flush the vibes, close the fd, say goodnight ๐Ÿ’…

๐Ÿ‘‘ RETURN VALUE ๐Ÿ‘‘

  • If it ate? ๐ŸŽ‰ You get 0 back. Successful completion, come through.
  • If it did not eat? You get EOF back, and errno is set to tell you what went wrong.
โš ๏ธ and here's the part you need to tattoo on your brain, fr fr: in either case, any further access to that stream โ€” including calling fclose() on it again โ€” is undefined behavior. She's closed. She's done. Do not touch her. Not even a little. ๐Ÿ˜ญ

๐Ÿ’€ ERRORS ๐Ÿ’€

  • EBADF โ€” the file descriptor sitting underneath stream is not valid. It's giving fake ID energy.

Also, fclose() can lowkey fail and set errno for any of the errors specified for close(2), write(2), or fflush(3). So if you're confused, go check those pages, bestie ๐Ÿซถ

๐ŸŒˆ ATTRIBUTES ๐ŸŒˆ

For the full explanation of what these terms even mean, go look at attributes(7).

  • Interface: fclose()
  • Attribute: Thread safety
  • Value: MT-Safe โœ…

she's thread safe!! she's out here handling multiple threads and never once making it weird. we stan ๐Ÿงš

โญ STANDARDS โญ

C11, POSIX.1-2008.

๐Ÿ“ HISTORY ๐Ÿ“

C89, POSIX.1-2001.

she's been doing this since before most of us were born, no cap.

๐ŸŒธ NOTES ๐ŸŒธ

Important little tea spill: fclose() only flushes the user-space buffers that the C library provides. That's it. That's all she does.

If you actually need your data physically stored on disk, the kernel buffers have to be flushed too โ€” for example with sync(2) or fsync(2). Don't skip this step unless you enjoy losing data, because the vibe check on "where did my file go" is absolutely brutal ๐Ÿ’€

๐Ÿ”ฅ SEE ALSO ๐Ÿ”ฅ


and that's the page!! this one came from Linux man-pages 6.20, dated 2025-09-21 โ€” so you know it's fresh, you know it's accurate, you know it ate ๐Ÿ’…โœจ bye bestie, go close your streams responsibly!! ๐Ÿฆ‹๐Ÿ’–

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: BSD-4-Clause-UC
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms