| 1 |
'\" t |
| 2 |
.\" Copyright 1990-1991, The Regents of the University of California. |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: BSD-4-Clause-UC |
| 6 |
.\" |
| 7 |
.TH fclose 3 2025-09-21 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
fclose \- close a stream |
| 10 |
.SH LIBRARY |
| 11 |
Standard C library |
| 12 |
.RI ( libc ,\~ \-lc ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.B #include <stdio.h> |
| 16 |
.P |
| 17 |
.BI "int fclose(FILE *" stream ); |
| 18 |
.fi |
| 19 |
.SH DESCRIPTION |
| 20 |
The |
| 21 |
.BR fclose () |
| 22 |
function flushes the stream pointed to by |
| 23 |
.I stream |
| 24 |
(writing any buffered output data using |
| 25 |
.BR fflush (3)) |
| 26 |
and closes the underlying file descriptor. |
| 27 |
.\" Reviewed by upstream and rejected, May 2012, Debian#67239 |
| 28 |
.PP |
| 29 |
The behaviour of |
| 30 |
.BR fclose () |
| 31 |
is undefined if the |
| 32 |
.I stream |
| 33 |
parameter is an illegal pointer, or is a descriptor already passed |
| 34 |
to a previous invocation of |
| 35 |
.BR fclose (). |
| 36 |
.\" End of patch |
| 37 |
.SH RETURN VALUE |
| 38 |
Upon successful completion, 0 is returned. |
| 39 |
Otherwise, |
| 40 |
.B EOF |
| 41 |
is returned and |
| 42 |
.I errno |
| 43 |
is set to indicate the error. |
| 44 |
In either case, any further access |
| 45 |
(including another call to |
| 46 |
.BR fclose ()) |
| 47 |
to the stream results in undefined behavior. |
| 48 |
.SH ERRORS |
| 49 |
.TP |
| 50 |
.B EBADF |
| 51 |
The file descriptor underlying |
| 52 |
.I stream |
| 53 |
is not valid. |
| 54 |
.\" This error cannot occur unless you are mixing ANSI C stdio operations and |
| 55 |
.\" low-level file operations on the same stream. If you do get this error, |
| 56 |
.\" you must have closed the stream's low-level file descriptor using |
| 57 |
.\" something like close(fileno(stream)). |
| 58 |
.P |
| 59 |
The |
| 60 |
.BR fclose () |
| 61 |
function may also fail and set |
| 62 |
.I errno |
| 63 |
for any of the errors specified for the routines |
| 64 |
.BR close (2), |
| 65 |
.BR write (2), |
| 66 |
or |
| 67 |
.BR fflush (3). |
| 68 |
.SH ATTRIBUTES |
| 69 |
For an explanation of the terms used in this section, see |
| 70 |
.BR attributes (7). |
| 71 |
.TS |
| 72 |
allbox; |
| 73 |
lbx lb lb |
| 74 |
l l l. |
| 75 |
Interface Attribute Value |
| 76 |
T{ |
| 77 |
.na |
| 78 |
.nh |
| 79 |
.BR fclose () |
| 80 |
T} Thread safety MT-Safe |
| 81 |
.TE |
| 82 |
.SH STANDARDS |
| 83 |
C11, POSIX.1-2008. |
| 84 |
.SH HISTORY |
| 85 |
C89, POSIX.1-2001. |
| 86 |
.SH NOTES |
| 87 |
Note that |
| 88 |
.BR fclose () |
| 89 |
flushes only the user-space buffers provided by the |
| 90 |
C library. |
| 91 |
To ensure that the data is physically stored |
| 92 |
on disk the kernel buffers must be flushed too, for example, with |
| 93 |
.BR sync (2) |
| 94 |
or |
| 95 |
.BR fsync (2). |
| 96 |
.SH SEE ALSO |
| 97 |
.BR close (2), |
| 98 |
.BR fcloseall (3), |
| 99 |
.BR fflush (3), |
| 100 |
.BR fileno (3), |
| 101 |
.BR fopen (3), |
| 102 |
.BR setbuf (3) |