close()

Updated: October 28, 2024

Close a file

Synopsis:

#include <unistd.h>

int close( int filedes );

Arguments:

filedes
The file descriptor of the file you want to close. This can be a file descriptor returned by a successful call to accept(), creat(), dup(), dup2(), fcntl(), modem_open(), open(), shm_open(), socket() or sopen().

Library:

libc

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

Description:

The close() function closes the file specified by the given file descriptor.

Returns:

Zero for success, or -1 if an error occurs (errno is set).

Errors:

EBADF
Invalid file descriptor filedes.
EINTR
The close() call was interrupted by a signal. In the QNX Neutrino implementation, the file descriptor remains open.
EIO
An I/O error occurred while updating the directory information.
ENOSPC
A previous buffered write call has failed.
ENOSYS
The close() function isn't implemented for the filesystem specified by filedes.

Examples:

#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main( void )
{
    int filedes;

    filedes = open( "file", O_RDONLY );
    if( filedes != -1 ) {
        /* process file */
        close( filedes );

        return EXIT_SUCCESS;
    }

    return EXIT_FAILURE;
}

Classification:

POSIX 1003.1

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