FREOPEN(3)                 POSIX Programmer's Manual                FREOPEN(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
       freopen -- open a stream

SSYYNNOOPPSSIISS
       #include <stdio.h>

       FILE *freopen(const char *restrict _p_a_t_h_n_a_m_e, const char *restrict _m_o_d_e,
           FILE *restrict _s_t_r_e_a_m);

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-2017
       defers to the ISO C standard.

       The _f_r_e_o_p_e_n() function shall first attempt to flush the stream
       associated with _s_t_r_e_a_m as if by a call to _f_f_l_u_s_h(_s_t_r_e_a_m).  Failure to
       flush the stream successfully shall be ignored. If _p_a_t_h_n_a_m_e is not a
       null pointer, _f_r_e_o_p_e_n() shall close any file descriptor associated with
       _s_t_r_e_a_m.  Failure to close the file descriptor successfully shall be
       ignored.  The error and end-of-file indicators for the stream shall be
       cleared.

       The _f_r_e_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 associate the stream pointed to by _s_t_r_e_a_m
       with it. The _m_o_d_e argument shall be used just as in _f_o_p_e_n().

       The original stream shall be closed regardless of whether the
       subsequent open succeeds.

       If _p_a_t_h_n_a_m_e is a null pointer, the _f_r_e_o_p_e_n() function shall attempt to
       change the mode of the stream to that specified by _m_o_d_e, as if the name
       of the file currently associated with the stream had been used. In this
       case, the file descriptor associated with the stream need not be closed
       if the call to _f_r_e_o_p_e_n() succeeds. It is implementation-defined which
       changes of mode are permitted (if any), and under what circumstances.

       After a successful call to the _f_r_e_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.

       If _p_a_t_h_n_a_m_e is not a null pointer, or if _p_a_t_h_n_a_m_e is a null pointer and
       the specified mode change necessitates the file descriptor associated
       with the stream to be closed and reopened, the file descriptor
       associated with the reopened stream shall be allocated and opened as if
       by a call to _o_p_e_n() with the following flags:

                   +-----------------+---------------------------+
                   | _f_r_e_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_r_e_o_p_e_n() shall return the value of _s_t_r_e_a_m.
       Otherwise, a null pointer shall be returned, and _e_r_r_n_o shall be set to
       indicate the error.

EERRRROORRSS
       The _f_r_e_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.

       EEBBAADDFF  The file descriptor underlying the stream is not a valid file
              descriptor when _p_a_t_h_n_a_m_e is a null pointer.

       EEIINNTTRR  A signal was caught during _f_r_e_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.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a component of a pathname is longer than
              {NAME_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 without the trailing <slash> characters would name
              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 it 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_r_e_o_p_e_n() function may fail if:

       EEBBAADDFF  The mode with which the file descriptor underlying the stream
              was opened does not support the requested mode when _p_a_t_h_n_a_m_e is
              a null pointer.

       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.

       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}.

       EENNOOMMEEMM Insufficient storage space is available.

       EENNXXIIOO  A request was made of a nonexistent device, or the request was
              outside the capabilities of the device.

       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
   DDiirreeccttiinngg SSttaannddaarrdd OOuuttppuutt ttoo aa FFiillee
       The following example logs all standard output to the //ttmmpp//llooggffiillee
       file.


           #include <stdio.h>
           ...
           FILE *fp;
           ...
           fp = freopen ("/tmp/logfile", "a+", stdout);
           ...

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       The _f_r_e_o_p_e_n() function is typically used to attach the pre-opened
       _s_t_r_e_a_m_s associated with _s_t_d_i_n, _s_t_d_o_u_t, and _s_t_d_e_r_r to other files.

       Since implementations are not required to support any stream mode
       changes when the _p_a_t_h_n_a_m_e argument is NULL, portable applications
       cannot rely on the use of _f_r_e_o_p_e_n() to change the stream mode, and use
       of this feature is discouraged. The feature was originally added to the
       ISO C standard in order to facilitate changing _s_t_d_i_n and _s_t_d_o_u_t to
       binary mode. Since a ''bb'' character in the mode has no effect on POSIX
       systems, this use of the feature is unnecessary in POSIX applications.
       However, even though the ''bb'' is ignored, a successful call to
       _f_r_e_o_p_e_n(NULL, "_w_b", _s_t_d_o_u_t) does have an effect. In particular, for
       regular files it truncates the file and sets the file-position
       indicator for the stream to the start of the file. It is possible that
       these side-effects are an unintended consequence of the way the feature
       is specified in the ISO/IEC 9899:1999 standard, but unless or until the
       ISO C standard is changed, applications which successfully call
       _f_r_e_o_p_e_n(NULL, "_w_b", _s_t_d_o_u_t) will behave in unexpected ways on
       conforming systems in situations such as:


           { appl file1; appl file2; } > file3

       which will result in ffiillee33 containing only the output from the second
       invocation of _a_p_p_l.

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, _f_c_l_o_s_e(), _f_d_o_p_e_n(), _f_f_l_u_s_h(),
       _f_m_e_m_o_p_e_n(), _f_o_p_e_n(), _m_b_s_i_n_i_t(), _o_p_e_n(), _o_p_e_n___m_e_m_s_t_r_e_a_m()

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

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  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.opengroup.org/unix/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                  2017                           FREOPEN(3)
