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() or setvbuf() 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 to fclose(), any use of stream results 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 return 0 πŸŽ‰
  • otherwise, it shall return EOF and set errno to indicate the error 😭

πŸ’€ ERRORS πŸ’€

the fclose() function shall fail if:

  • EAGAIN β€” the O_NONBLOCK flag is set for the file descriptor underlying stream and the thread would be delayed in the write operation.
  • EBADF β€” the file descriptor underlying stream is 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 β€” the fclose() function was interrupted by a signal.
  • EIO β€” the process is a member of a background process group attempting to write to its controlling terminal, TOSTOP is set, the calling thread is not blocking SIGTTOU, the process is not ignoring SIGTTOU, 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 by open_memstream() or open_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 the fmemopen() function.
  • EPIPE β€” an attempt is made to write to a pipe or FIFO that is not open for reading by any process. a SIGPIPE signal 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>

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