![]() |
![]() |
![]() |
Associate a stream with a file descriptor
#include <stdio.h> FILE* fdopen( int filedes, const char* mode );
libc
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().
A file stream for success, or NULL if an error occurs (errno is set).
#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; }
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
creat(), dup(), dup2(), errno, fcntl(), fopen(), freopen(), open(), pipe(), sopen()
![]() |
![]() |
![]() |