1FCLOSE(3P)                 POSIX Programmer's Manual                FCLOSE(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       fclose — close a stream
13

SYNOPSIS

15       #include <stdio.h>
16
17       int fclose(FILE *stream);
18

DESCRIPTION

20       The functionality described on this reference page is aligned with  the
21       ISO C  standard.  Any  conflict between the requirements described here
22       and the ISO C standard is unintentional. This  volume  of  POSIX.1‐2017
23       defers to the ISO C standard.
24
25       The fclose() function shall cause the stream pointed to by stream to be
26       flushed and the associated file to be closed.  Any  unwritten  buffered
27       data  for  the stream shall be written to the file; any unread buffered
28       data shall be discarded. Whether or not the call succeeds,  the  stream
29       shall be disassociated from the file and any buffer set by the setbuf()
30       or setvbuf() function shall be disassociated from the  stream.  If  the
31       associated buffer was automatically allocated, it shall be deallocated.
32
33       If the file is not already at EOF, and the file is one capable of seek‐
34       ing, the file offset of the underlying open file description  shall  be
35       set to the file position of the stream if the stream is the active han‐
36       dle to the underlying file description.
37
38       The fclose() function shall mark for update the last data  modification
39       and  last  file status change timestamps of the underlying file, if the
40       stream was writable, and if buffered data remains that has not yet been
41       written to the file. The fclose() function shall perform the equivalent
42       of a close() on the file descriptor that is associated with the  stream
43       pointed to by stream.
44
45       After  the  call  to  fclose(),  any use of stream results in undefined
46       behavior.
47

RETURN VALUE

49       Upon successful completion, fclose()  shall  return  0;  otherwise,  it
50       shall return EOF and set errno to indicate the error.
51

ERRORS

53       The fclose() function shall fail if:
54
55       EAGAIN The  O_NONBLOCK  flag  is set for the file descriptor underlying
56              stream and the thread would be delayed in the write operation.
57
58       EBADF  The file descriptor underlying stream is not valid.
59
60       EFBIG  An attempt was made to write a file  that  exceeds  the  maximum
61              file size.
62
63       EFBIG  An  attempt  was made to write a file that exceeds the file size
64              limit of the process.
65
66       EFBIG  The file is a regular file and an attempt was made to  write  at
67              or  beyond  the offset maximum associated with the corresponding
68              stream.
69
70       EINTR  The fclose() function was interrupted by a signal.
71
72       EIO    The process is a member of a background process group attempting
73              to write to its controlling terminal, TOSTOP is set, the calling
74              thread is not blocking SIGTTOU,  the  process  is  not  ignoring
75              SIGTTOU, and the process group of the process is orphaned.  This
76              error may also be returned under  implementation-defined  condi‐
77              tions.
78
79       ENOMEM The   underlying  stream  was  created  by  open_memstream()  or
80              open_wmemstream() and insufficient memory is available.
81
82       ENOSPC There was no free space remaining on the device  containing  the
83              file or in the buffer used by the fmemopen() function.
84
85       EPIPE  An  attempt  is made to write to a pipe or FIFO that is not open
86              for reading by any process. A SIGPIPE signal shall also be  sent
87              to the thread.
88
89       The fclose() function may fail if:
90
91       ENXIO  A  request  was made of a nonexistent device, or the request was
92              outside the capabilities of the device.
93
94       The following sections are informative.
95

EXAMPLES

97       None.
98

APPLICATION USAGE

100       Since after the call to fclose() any use of stream results in undefined
101       behavior,  fclose()  should  not  be  used  on stdin, stdout, or stderr
102       except immediately before process termination (see the Base Definitions
103       volume  of  POSIX.1‐2017, Section 3.303, Process Termination), so as to
104       avoid triggering undefined behavior in other standard  interfaces  that
105       rely on these streams. If there are any atexit() handlers registered by
106       the application, such a call to fclose() should  not  occur  until  the
107       last  handler is finishing. Once fclose() has been used to close stdin,
108       stdout, or stderr, there is no standard way  to  reopen  any  of  these
109       streams.
110
111       Use  of freopen() to change stdin, stdout, or stderr instead of closing
112       them avoids the danger of a file unexpectedly being opened  as  one  of
113       the   special   file   descriptors   STDIN_FILENO,   STDOUT_FILENO,  or
114       STDERR_FILENO at a later time in the application.
115

RATIONALE

117       None.
118

FUTURE DIRECTIONS

120       None.
121

SEE ALSO

123       Section 2.5,  Standard  I/O  Streams,  atexit(),  close(),  fmemopen(),
124       fopen(), freopen(), getrlimit(), open_memstream(), ulimit()
125
126       The Base Definitions volume of POSIX.1‐2017, <stdio.h>
127
129       Portions  of  this text are reprinted and reproduced in electronic form
130       from IEEE Std 1003.1-2017, Standard for Information Technology --  Por‐
131       table  Operating System Interface (POSIX), The Open Group Base Specifi‐
132       cations Issue 7, 2018 Edition, Copyright (C) 2018 by the  Institute  of
133       Electrical  and  Electronics Engineers, Inc and The Open Group.  In the
134       event of any discrepancy between this version and the original IEEE and
135       The  Open Group Standard, the original IEEE and The Open Group Standard
136       is the referee document. The original Standard can be  obtained  online
137       at http://www.opengroup.org/unix/online.html .
138
139       Any  typographical  or  formatting  errors that appear in this page are
140       most likely to have been introduced during the conversion of the source
141       files  to  man page format. To report such errors, see https://www.ker
142       nel.org/doc/man-pages/reporting_bugs.html .
143
144
145
146IEEE/The Open Group                  2017                           FCLOSE(3P)
Impressum