[Previous] [Contents] [Next]

fdopen()

Associate a stream with a file descriptor

Synopsis:

#include <stdio.h>

FILE* fdopen( int filedes, 
              const char* mode );

Library:

libc

Description:

The fdopen() function associates a stream with the file descriptor filedes, which represents an opened file or device.

The filedes argument is a file descriptor that was returned by one of accept(), creat(), dup(), dup2(), fcntl(), open(), pipe(), or sopen().

The open mode must match the mode specified when filedes was originally opened. For information about the mode, see fopen().

Returns:

A file stream for success, or NULL if an error occurs (errno is set).

Examples:

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

int main( void )
{
    int filedes ;
    FILE *fp;

    filedes = open( "file", O_RDONLY );
    if( filedes != -1 ) {
        fp = fdopen( filedes , "r" );
        if( fp != NULL ) {
            /* Also closes the underlying FD, filedes. */
            fclose( fp );
        }
    }
    return EXIT_SUCCESS;
}

Classification:

POSIX 1003.1

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

See also:

creat(), dup(), dup2(), errno, fcntl(), fopen(), freopen(), open(), pipe(), sopen()


[Previous] [Contents] [Next]