fclose()

Close a stream

Synopsis:

#include <stdio.h>

int fclose( FILE* fp );

Arguments:

fp
The stream you want to close.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The fclose() function closes the stream specified by fp. Any unwritten, buffered data is flushed before the file is closed. Any unread, buffered data is discarded.

If the associated buffer was automatically allocated, it's deallocated.

Returns:

0 for success, or EOF if an error occurred (errno is set).

Errors:

EAGAIN
The O_NONBLOCK flag is set for the file descriptor underlying fp, and the process would be delayed in the write operation.
EBADF
The file descriptor underlying fp isn't valid.
EFBIG
One of the following:
EINTR
The fclose() function was interrupted by a signal.
EIO
One of the following:
ENOSPC
There was no free space remaining on the device containing the file.
ENXIO
A request was made of a nonexistent device, or the request was outside the capabilities of the device.
EPIPE
An attempt was made to write to a pipe or FIFO that wasn't open for reading by any process. A SIGPIPE signal is also sent to the thread.

Examples:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
    FILE *fp;

    fp = fopen( "stdio.h", "r" );
    if( fp != NULL ) {
        fclose( fp );
        
        return EXIT_SUCCESS;
    }
    
    return EXIT_FAILURE;
}

Classification:

ANSI, POSIX 1003.1

Safety:
Cancellation point Yes
Interrupt handler No
Signal handler No
Thread Yes

See also:

errno, fcloseall(), fdopen(), fopen(), freopen()