FCLOSE(3P)
π FCLOSE(3P) β a POSIX Programmer's Manual moment π
okay bestie, welcome to FCLOSE(3P), straight out of the POSIX Programmer's Manual. this one's short but she's important, so grab your strawberry drink and let's go π
πΈ PROLOG πΈ
real talk first: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ β so if you're on Linux, go consult the corresponding Linux manual page for the deets on Linux behavior. orrr the interface might just not be implemented on Linux at all π. vibe check accordingly.
β¨ NAME β¨
fclose β close a stream π
π¦ SYNOPSIS π¦
#include <stdio.h>
int fclose(FILE *stream);
π DESCRIPTION π
so the functionality on this reference page is aligned with the ISO C standard. if there's any conflict between what's written here and the ISO C standard, that conflict is unintentional β no drama intended, fr fr. this volume of POSIX.1β2017 defers to the ISO C standard. periodt.
now the actual tea β β the fclose() function shall cause the stream pointed to by stream to be flushed and the associated file to be closed. here's the breakdown:
- any unwritten buffered data for the stream shall be written to the file β
- any unread buffered data shall be discarded ποΈ
- whether or not the call succeeds, the stream shall be disassociated from the file
- any buffer set by the
setbuf()orsetvbuf()function shall be disassociated from the stream - if the associated buffer was automatically allocated, it shall be deallocated
if the file is not already at EOF, and the file is one capable of seeking, the file offset of the underlying open file description shall be set to the file position of the stream if the stream is the active handle to the underlying file description. iykyk π
the fclose() function shall mark for update the last data modification and last file status change timestamps of the underlying file, if the stream was writable, and if buffered data remains that has not yet been written to the file.
and fclose() shall perform the equivalent of a close() on the file descriptor that is associated with the stream pointed to by stream. cute, she multitasks π
π¨ BIG WARNING BESTIE π¨ after the call tofclose(), any use ofstreamresults in undefined behavior. that's not a drill, that's undefined behavior. do not touch her after this.
π₯ RETURN VALUE π₯
- upon successful completion,
fclose()shall return0π - otherwise, it shall return
EOFand seterrnoto indicate the error π
π ERRORS π
the fclose() function shall fail if:
EAGAINβ theO_NONBLOCKflag is set for the file descriptor underlyingstreamand the thread would be delayed in the write operation.EBADFβ the file descriptor underlyingstreamis not valid.EFBIGβ an attempt was made to write a file that exceeds the maximum file size.EFBIGβ an attempt was made to write a file that exceeds the file size limit of the process.EFBIGβ the file is a regular file and an attempt was made to write at or beyond the offset maximum associated with the corresponding stream.EINTRβ thefclose()function was interrupted by a signal.EIOβ the process is a member of a background process group attempting to write to its controlling terminal,TOSTOPis set, the calling thread is not blockingSIGTTOU, the process is not ignoringSIGTTOU, and the process group of the process is orphaned. this error may also be returned under implementation-defined conditions.ENOMEMβ the underlying stream was created byopen_memstream()oropen_wmemstream()and insufficient memory is available.ENOSPCβ there was no free space remaining on the device containing the file or in the buffer used by thefmemopen()function.EPIPEβ an attempt is made to write to a pipe or FIFO that is not open for reading by any process. aSIGPIPEsignal shall also be sent to the thread.
the fclose() function may fail if:
ENXIOβ a request was made of a nonexistent device, or the request was outside the capabilities of the device.
heads up: the following sections are informative. (yes, they said it themselves, we're just passing the message along π«Ά)
π§ EXAMPLES π§
None. (iconic of her honestly, no notes)
π APPLICATION USAGE π
since after the call to fclose() any use of stream results in undefined behavior, fclose() should not be used on stdin, stdout, or stderr except immediately before process termination β see the Base Definitions volume of POSIX.1β2017, Section 3.303, Process Termination. this is so you avoid triggering undefined behavior in other standard interfaces that rely on these streams π
if there are any atexit() handlers registered by the application, such a call to fclose() should not occur until the last handler is finishing.
and once fclose() has been used to close stdin, stdout, or stderr, there is no standard way to reopen any of these streams. gone. no take-backs. π
use freopen() to change stdin, stdout, or stderr instead of closing them β that avoids the danger of a file unexpectedly being opened as one of the special file descriptors STDIN_FILENO, STDOUT_FILENO, or STDERR_FILENO at a later time in the application. β¨ bestie behavior, honestly β¨
π RATIONALE π
None.
β FUTURE DIRECTIONS β
None.
π SEE ALSO π
- Section 2.5, Standard I/O Streams
atexit()close()fmemopen()fopen()freopen()getrlimit()open_memstream()ulimit()- The Base Definitions volume of POSIX.1β2017,
<stdio.h>
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group.
in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page bestie, no crumbs left π
this came to you from IEEE/The Open Group, 2017, and yes the footer says FCLOSE(3P) because she's that girl. now go close your streams responsibly, mwah π«Άπβ¨
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- 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-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms