Associate a stream with a file descriptor
#include <stdio.h> FILE* fdopen( int filedes, const char* mode );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
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 fdopen() function preserves the offset maximum previously set for the open file description corresponding to filedes.
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()