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
0back. Successful completion, come through. - If it did not eat? You get
EOFback, anderrnois 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 underneathstreamis 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
- Copyright 1990-1991, The Regents of the University of California
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms