associate a stream with a file descriptor
#include <stdio.h> FILE *fdopen( int filedes, const char *mode );
The fdopen() function associates a stream with the file descriptor filedes, which represents an opened file or device. The descriptor was returned by one of creat(), dup(), dup2(), fcntl(), open(), pipe(), or sopen(). The open mode mode must match the mode with which the file or device was originally opened.
The argument mode is described in the description of the fopen() function.
A pointer to the object controlling the stream. This pointer must be passed as a parameter to subsequent functions for performing operations on the file. If the open operation fails, fdopen() returns a NULL pointer. When an error has occurred, errno contains a value indicating the type of error that has been detected.
#include <stdio.h> #include <fcntl.h> #include <unistd.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 0; }
POSIX 1003.1
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
creat(), dup(), dup2(), errno, fcntl(), fopen(), freopen(), _fsopen(), open(), pipe(), sopen()