FCLOSE(3S) SysV FCLOSE(3S)
NAME
fclose, fflush - close or flush a stream
SYNOPSIS
#include <stdio.h>
int fclose (stream)
FILE *stream;
int fflush (stream)
FILE *stream;
DESCRIPTION
fclose causes any buffered data for the named stream to be written out,
and the stream to be closed. It is automatically called for all open
files when the exit(2) function is invoked.
Any unwritten buffered data for the stream is delivered to the host
environment to be written to the file; any unread buffered data is
discarded. The stream is disassociated from the file. If the associated
buffer was automatically allocated, it is deallocated. Any further use
of the stream specified by the stream parameter causes undefined
behavior.
The fclose function performs close(2) on the file descriptor associated
with the stream parameter.
The fflush call causes any buffered data for the named stream to be
written to that file. The stream remains open.
If stream is a null pointer, the fflush function performs this flushing
action on all streams for which the behavior is defined above.
For fclose, If the stream was writable and buffered data was not yet
written to the file, it marks the st_ctime and st_mtime fields of the
underlying file for update.
For fflush, the st_ctime and st_mtime fields of the underlying file are
marked for update. If the stream is open for reading, any unread data
buffered in the stream is discarded.
In an AES, POSIX, or XPG3 environment, the following is true for the
fclose and fflush calls:
if the file is not already at EOF, and is capable of seeking (or, in
the case of fflush,
is open for reading), the file pointer (or file offset) of the
underlying open file description is adjusted so that the next
operation on the open file description deals with the byte after the
last one read from or written to the stream being closed.
DIAGNOSTICS
Upon successful completion, both the fclose and fflush functions return a
value of 0 (zero). Otherwise, EOF is returned and errno is set to
indicate the error.
ERRORS
If the fclose function fails, errno is set to one of the following
values:
[EBADF] The file descriptor underlying the stream parameter is not
valid.
[EINTR] The fclose function was interrupted by a signal which was
caught.
[EAGAIN] The O_NONBLOCK flag is set for the file descriptor
underlying the stream parameter and the process would be
delayed in the write operation.
[EFBIG] An attempt was made to write a file that exceeds the
process' file size limit or the maximum file size. See
the ulimit function.
[EIO] The process is a member of a background process group
attempting to write to its controlling terminal, TOSTOP is
set, the process is neither ignoring nor blocking SIGTTOU
and the process group of the process is orphaned. This
error may also be returned under implementation-defined
conditions.
[ENOSPC] There was no free space remaining on the device containing
the file.
[EPIPE] An attempt was made to write to a pipe or FIFO that is not
open for reading by any process. A SIGPIPE signal will
also be sent to the process.
If the fflush function fails, errno is set to one of the following
values:
[EAGAIN] The O_NONBLOCK flag is set for the file descriptor
underlying stream and the process would be delayed in the
write operation.
[EBADF] The file descriptor underlying the stream parameter is not
valid.
[EFBIG] An attempt was made to write a file that exceeds the
process' file size limit or the maximum file size. See
the ulimit function.
[EINTR] The fflush function was interrupted by a signal which was
caught.
[EIO] The implementation supports job control, the process is a
member of a background process group attempting to write
to its controlling terminal, TOSTOP is set, the process is
neither ignoring nor blocking SIGTTOU and the process
group of the process is orphaned. This error may also be
returned under implementation-defined conditions.
[ENOSPC] There was no free space remaining on the device containing
the file.
[EPIPE] An attempt is made to write to a pipe or FIFO that is not
open for reading by any process. A SIGPIPE signal will
also be sent to the process.
SEE ALSO
close(2), exit(2), fopen(3S), setbuf(3S), stdio.h.