FOPEN(3)                   POSIX Programmer's Manual                  FOPEN(3)



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


NNAAMMEE
       fopen -- open a stream

SSYYNNOOPPSSIISS
       #include <stdio.h>

       FILE *fopen(const char *restrict _p_a_t_h_n_a_m_e, const char *restrict _m_o_d_e);

DDEESSCCRRIIPPTTIIOONN
       The functionality described on this reference page is aligned with the
       ISO C standard. Any conflict between the requirements described here
       and the ISO C standard is unintentional. This volume of POSIX.1-2008
       defers to the ISO C standard.

       The _f_o_p_e_n() function shall open the file whose pathname is the string
       pointed to by _p_a_t_h_n_a_m_e, and associates a stream with it.

       The _m_o_d_e argument points to a string. If the string is one of the
       following, the file shall be opened in the indicated mode. Otherwise,
       the behavior is undefined.

       _r or _r_b       Open file for reading.

       _w or _w_b       Truncate to zero length or create file for writing.

       _a or _a_b       Append; open or create file for writing at end-of-file.

       _r_+ or _r_b_+ or _r_+_b
                     Open file for update (reading and writing).

       _w_+ or _w_b_+ or _w_+_b
                     Truncate to zero length or create file for update.

       _a_+ or _a_b_+ or _a_+_b
                     Append; open or create file for update, writing at end-
                     of-file.

       The character ''bb'' shall have no effect, but is allowed for ISO C
       standard conformance.  Opening a file with read mode (_r as the first
       character in the _m_o_d_e argument) shall fail if the file does not exist
       or cannot be read.

       Opening a file with append mode (_a as the first character in the _m_o_d_e
       argument) shall cause all subsequent writes to the file to be forced to
       the then current end-of-file, regardless of intervening calls to
       _f_s_e_e_k().

       When a file is opened with update mode (''++'' as the second or third
       character in the _m_o_d_e argument), both input and output may be performed
       on the associated stream. However, the application shall ensure that
       output is not directly followed by input without an intervening call to
       _f_f_l_u_s_h() or to a file positioning function (_f_s_e_e_k(), _f_s_e_t_p_o_s(), or
       _r_e_w_i_n_d()), and input is not directly followed by output without an
       intervening call to a file positioning function, unless the input
       operation encounters end-of-file.

       When opened, a stream is fully buffered if and only if it can be
       determined not to refer to an interactive device. The error and end-of-
       file indicators for the stream shall be cleared.

       If _m_o_d_e is _w, _w_b, _a, _a_b, _w+, _w_b+, _w+_b, _a+, _a_b+, or _a+_b, and the file
       did not previously exist, upon successful completion, _f_o_p_e_n() shall
       mark for update the last data access, last data modification, and last
       file status change timestamps of the file and the last file status
       change and last data modification timestamps of the parent directory.

       If _m_o_d_e is _w, _w_b, _a, _a_b, _w+, _w_b+, _w+_b, _a+, _a_b+, or _a+_b, and the file
       did not previously exist, the _f_o_p_e_n() function shall create a file as
       if it called the _c_r_e_a_t() function with a value appropriate for the _p_a_t_h
       argument interpreted from _p_a_t_h_n_a_m_e and a value of S_IRUSR | S_IWUSR |
       S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH for the _m_o_d_e argument.

       If _m_o_d_e is _w, _w_b, _w+, _w_b+, or _w+_b, and the file did previously exist,
       upon successful completion, _f_o_p_e_n() shall mark for update the last data
       modification and last file status change timestamps of the file.

       After a successful call to the _f_o_p_e_n() function, the orientation of the
       stream shall be cleared, the encoding rule shall be cleared, and the
       associated mmbbssttaattee__tt object shall be set to describe an initial
       conversion state.

       The file descriptor associated with the opened stream shall be
       allocated and opened as if by a call to _o_p_e_n() with the following
       flags:

                   +-----------------+---------------------------+
                   |  _f_o_p_e_n(()) MMooddee   |       _o_p_e_n(()) FFllaaggss        |
                   +-----------------+---------------------------+
                   |_r or _r_b          | O_RDONLY                  |
                   |_w or _w_b          | O_WRONLY|O_CREAT|O_TRUNC  |
                   |_a or _a_b          | O_WRONLY|O_CREAT|O_APPEND |
                   |_r_+ or _r_b_+ or _r_+_b | O_RDWR                    |
                   |_w_+ or _w_b_+ or _w_+_b | O_RDWR|O_CREAT|O_TRUNC    |
                   |_a_+ or _a_b_+ or _a_+_b | O_RDWR|O_CREAT|O_APPEND   |
                   +-----------------+---------------------------+
RREETTUURRNN VVAALLUUEE
       Upon successful completion, _f_o_p_e_n() shall return a pointer to the
       object controlling the stream. Otherwise, a null pointer shall be
       returned, and _e_r_r_n_o shall be set to indicate the error.

EERRRROORRSS
       The _f_o_p_e_n() function shall fail if:

       EEAACCCCEESS Search permission is denied on a component of the path prefix,
              or the file exists and the permissions specified by _m_o_d_e are
              denied, or the file does not exist and write permission is
              denied for the parent directory of the file to be created.

       EEIINNTTRR  A signal was caught during _f_o_p_e_n().

       EEIISSDDIIRR The named file is a directory and _m_o_d_e requires write access.

       EELLOOOOPP  A loop exists in symbolic links encountered during resolution of
              the _p_a_t_h argument.

       EEMMFFIILLEE All file descriptors available to the process are currently
              open.

       EEMMFFIILLEE {STREAM_MAX} streams are currently open in the calling process.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a pathname exceeds {PATH_MAX}, or pathname
              resolution of a symbolic link produced an intermediate result
              with a length that exceeds {PATH_MAX}.

       EENNFFIILLEE The maximum allowable number of files is currently open in the
              system.

       EENNOOEENNTT The _m_o_d_e string begins with ''rr'' and a component of _p_a_t_h_n_a_m_e does
              not name an existing file, or _m_o_d_e begins with ''ww'' or ''aa'' and a
              component of the path prefix of _p_a_t_h_n_a_m_e does not name an
              existing file, or _p_a_t_h_n_a_m_e is an empty string.

       EENNOOEENNTT or EENNOOTTDDIIRR
              The _p_a_t_h_n_a_m_e argument contains at least one non-<slash>
              character and ends with one or more trailing <slash> characters.
              If _p_a_t_h_n_a_m_e names an existing file, an [[EENNOOEENNTT]] error shall not
              occur.

       EENNOOSSPPCC The directory or file system that would contain the new file
              cannot be expanded, the file does not exist, and the file was to
              be created.

       EENNOOTTDDIIRR
              A component of the path prefix names an existing file that is
              neither a directory nor a symbolic link to a directory, or the
              _p_a_t_h_n_a_m_e argument contains at least one non-<slash> character
              and ends with one or more trailing <slash> characters and the
              last pathname component names an existing file that is neither a
              directory nor a symbolic link to a directory.

       EENNXXIIOO  The named file is a character special or block special file, and
              the device associated with this special file does not exist.

       EEOOVVEERRFFLLOOWW
              The named file is a regular file and the size of the file cannot
              be represented correctly in an object of type ooffff__tt.

       EERROOFFSS  The named file resides on a read-only file system and _m_o_d_e
              requires write access.

       The _f_o_p_e_n() function may fail if:

       EEIINNVVAALL The value of the _m_o_d_e argument is not valid.

       EELLOOOOPP  More than {SYMLOOP_MAX} symbolic links were encountered during
              resolution of the _p_a_t_h argument.

       EEMMFFIILLEE {FOPEN_MAX} streams are currently open in the calling process.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a component of a pathname is longer than
              {NAME_MAX}.

       EENNOOMMEEMM Insufficient storage space is available.

       EETTXXTTBBSSYY
              The file is a pure procedure (shared text) file that is being
              executed and _m_o_d_e requires write access.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
   OOppeenniinngg aa FFiillee
       The following example tries to open the file named ffiillee for reading.
       The _f_o_p_e_n() function returns a file pointer that is used in subsequent
       _f_g_e_t_s() and _f_c_l_o_s_e() calls. If the program cannot open the file, it
       just ignores it.

           ##iinncclluuddee <<ssttddiioo..hh>>
           ......
           FFIILLEE **ffpp;;
           ......
           vvooiidd rrggrreepp((ccoonnsstt cchhaarr **ffiillee))
           {{
           ......
               iiff ((((ffpp == ffooppeenn((ffiillee,, ""rr"")))) ==== NNUULLLL))
                   rreettuurrnn;;
           ......
           }}

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       None.

RRAATTIIOONNAALLEE
       None.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _S_e_c_t_i_o_n _2_._5, _S_t_a_n_d_a_r_d _I_/_O _S_t_r_e_a_m_s, _c_r_e_a_t(), _f_c_l_o_s_e(), _f_d_o_p_e_n(),
       _f_m_e_m_o_p_e_n(), _f_r_e_o_p_e_n(), _o_p_e_n___m_e_m_s_t_r_e_a_m()

       The Base Definitions volume of POSIX.1-2008, <<ssttddiioo..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
       event of any discrepancy between this version and the original IEEE and
       The Open Group Standard, the original IEEE and The Open Group Standard
       is the referee document. The original Standard can be obtained online
       at http://www.unix.org/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2013                             FOPEN(3)
