FCLOSE(3)

NAME
fclose, fdclose - close a stream

SYNOPSIS
#include <stdio.h>
int
fclose(FILE *stream);
int
fdclose(FILE *stream, int *fdp);

DESCRIPTION
The fclose() function dissociates the named stream from its underlying file or set of functions. If the stream was being used for output then any buffered data is written first, while if the stream was being used for input then the underlying file position may be updated, as if via fflush(3).

The fdclose() function is equivalent to fclose() except that it does not close the associated file descriptor. If fdp is not NULL, the file descriptor will be stored through it. If the stream was created with a function other than fopen(3), fdopen(3), or freopen(3) and as a result does not have an associated file descriptor then -1 will stored through fdp.

RETURN VALUES
Upon successful completion 0 is returned. Otherwise, EOF is returned and the global variable errno is set to indicate the error. In either case no further access to the stream is possible.

ERRORS
The fclose() and fdclose() functions will fail if

[EBADF]
    The argument stream is not an open stream.

The fdclose() function will fail if

[EOPNOTSUPP]
    The argument stream does not have an associated file descriptor.

They may also fail and set errno for any of the errors specified for the fflush(3) routine.

The fclose() function may also fail and set errno for any of the errors specified for the close(2) routine.

SEE ALSO
close(2), fflush(3), fopen(3), setvbuf(3)

STANDARDS
The fclose() function conforms to p1003.1-2024.

HISTORY
The fclose() function first appeared in AT&T UNIX.

The fdclose() function first appeared in FreeBSD 11.0.