# fdopen

## PROLOG



           This manual page is part of the POSIX Programmer's Manual.  The
           Linux implementation of this interface may differ (consult the
           corresponding Linux manual page for details of Linux behavior), or
           the interface may not be implemented on Linux.
    


## NAME



           fdopen — associate a stream with a file descriptor
    


## SYNOPSIS



           #include <stdio.h>
    
           FILE *fdopen(int _fildes_ , const char *_mode_);
    


## DESCRIPTION



           The _fdopen_() function shall associate a stream with a file
           descriptor.
    
           The _mode_ argument is a character string having one of the
           following values:
    
           _r_ or _rb_       Open a file for reading.
    
           _w_ or _wb_       Open a file for writing.
    
           _a_ or _ab_       Open a file for writing at end-of-file.
    
           _r_ + or _rb_ + or _r_ +_b_
                         Open a file for update (reading and writing).
    
           _w_ + or _wb_ + or _w_ +_b_
                         Open a file for update (reading and writing).
    
           _a_ + or _ab_ + or _a_ +_b_
                         Open a file for update (reading and writing) at end-
                         of-file.
    
           The meaning of these flags is exactly as specified in _fopen_(),
           except that modes beginning with _w_ shall not cause truncation of
           the file.
    
           Additional values for the _mode_ argument may be supported by an
           implementation.
    
           The application shall ensure that the mode of the stream as
           expressed by the _mode_ argument is allowed by the file access mode
           of the open file description to which _fildes_ refers. The file
           position indicator associated with the new stream is set to the
           position indicated by the file offset associated with the file
           descriptor.
    
           The error and end-of-file indicators for the stream shall be
           cleared.  The _fdopen_() function may cause the last data access
           timestamp of the underlying file to be marked for update.
    
           If _fildes_ refers to a shared memory object, the result of the
           _fdopen_() function is unspecified.
    
           If _fildes_ refers to a typed memory object, the result of the
           _fdopen_() function is unspecified.
    
           The _fdopen_() function shall preserve the offset maximum previously
           set for the open file description corresponding to _fildes_.
    


## RETURN VALUE



           Upon successful completion, _fdopen_() shall return a pointer to a
           stream; otherwise, a null pointer shall be returned and _errno_ set
           to indicate the error.
    


## ERRORS



           The _fdopen_() function shall fail if:
    
           **EMFILE**{STREAM_MAX} streams are currently open in the calling
                  process.
    
           The _fdopen_() function may fail if:
    
           **EBADF** The _fildes_ argument is not a valid file descriptor.
    
           **EINVAL** The _mode_ argument is not a valid mode.
    
           **EMFILE**{FOPEN_MAX} streams are currently open in the calling
                  process.
    
           **ENOMEM** Insufficient space to allocate a buffer.
    
           _The following sections are informative._
    


## EXAMPLES



           None.
    


## APPLICATION USAGE



           File descriptors are obtained from calls like _open_(), _dup_(),
           _creat_(), or _pipe_(), which open files but do not return streams.
    


## RATIONALE



           The file descriptor may have been obtained from _open_(), _creat_(),
           _pipe_(), _dup_(), _fcntl_(), or _socket_(); inherited through _fork_(),
           _posix_spawn_(), or _exec_ ; or perhaps obtained by other means.
    
           The meanings of the _mode_ arguments of _fdopen_() and _fopen_() differ.
           With _fdopen_(), open for write (_w_ or _w+_) does not truncate, and
           append (_a_ or _a+_) cannot create for writing. The _mode_ argument
           formats that include a _b_ are allowed for consistency with the
           ISO C standard function _fopen_().  The _b_ has no effect on the
           resulting stream. Although not explicitly required by this volume
           of POSIX.1‐2017, a good implementation of append (_a_) mode would
           cause the O_APPEND flag to be set.
    


## FUTURE DIRECTIONS



           None.
    


* * *