1FREOPEN(3P)                POSIX Programmer's Manual               FREOPEN(3P)
2
3
4

PROLOG

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

NAME

12       freopen — open a stream
13

SYNOPSIS

15       #include <stdio.h>
16
17       FILE *freopen(const char *restrict pathname, const char *restrict mode,
18           FILE *restrict stream);
19

DESCRIPTION

21       The functionality described on this reference page is aligned with  the
22       ISO C  standard.  Any  conflict between the requirements described here
23       and the ISO C standard is unintentional. This  volume  of  POSIX.1‐2017
24       defers to the ISO C standard.
25
26       The  freopen() function shall first attempt to flush the stream associ‐
27       ated with stream as if by a call to fflush(stream).  Failure  to  flush
28       the  stream  successfully  shall  be ignored. If pathname is not a null
29       pointer, freopen() shall close  any  file  descriptor  associated  with
30       stream.   Failure  to  close  the file descriptor successfully shall be
31       ignored.  The error and end-of-file indicators for the stream shall  be
32       cleared.
33
34       The freopen() function shall open the file whose pathname is the string
35       pointed to by pathname and associate the stream pointed  to  by  stream
36       with it. The mode argument shall be used just as in fopen().
37
38       The  original  stream  shall be closed regardless of whether the subse‐
39       quent open succeeds.
40
41       If pathname is a null pointer, the freopen() function shall attempt  to
42       change the mode of the stream to that specified by mode, as if the name
43       of the file currently associated with the stream had been used. In this
44       case, the file descriptor associated with the stream need not be closed
45       if the call to freopen() succeeds. It is  implementation-defined  which
46       changes of mode are permitted (if any), and under what circumstances.
47
48       After  a  successful call to the freopen() function, the orientation of
49       the stream shall be cleared, the encoding rule shall  be  cleared,  and
50       the  associated  mbstate_t  object  shall be set to describe an initial
51       conversion state.
52
53       If pathname is not a null pointer, or if pathname is a null pointer and
54       the  specified  mode change necessitates the file descriptor associated
55       with the stream to be closed and reopened, the file descriptor  associ‐
56       ated  with the reopened stream shall be allocated and opened as if by a
57       call to open() with the following flags:
58
59                   ┌─────────────────┬───────────────────────────┐
60freopen() Mode  open() Flags        
61                   ├─────────────────┼───────────────────────────┤
62r or rb          │ O_RDONLY                  │
63w or wb          │ O_WRONLY|O_CREAT|O_TRUNC  │
64a or ab          │ O_WRONLY|O_CREAT|O_APPEND │
65r+ or rb+ or r+b │ O_RDWR                    │
66w+ or wb+ or w+b │ O_RDWR|O_CREAT|O_TRUNC    │
67a+ or ab+ or a+b │ O_RDWR|O_CREAT|O_APPEND   │
68                   └─────────────────┴───────────────────────────┘

RETURN VALUE

70       Upon successful completion, freopen() shall return the value of stream.
71       Otherwise,  a null pointer shall be returned, and errno shall be set to
72       indicate the error.
73

ERRORS

75       The freopen() function shall fail if:
76
77       EACCES Search permission is denied on a component of the  path  prefix,
78              or  the  file  exists  and the permissions specified by mode are
79              denied, or the file does  not  exist  and  write  permission  is
80              denied for the parent directory of the file to be created.
81
82       EBADF  The  file  descriptor  underlying the stream is not a valid file
83              descriptor when pathname is a null pointer.
84
85       EINTR  A signal was caught during freopen().
86
87       EISDIR The named file is a directory and mode requires write access.
88
89       ELOOP  A loop exists in symbolic links encountered during resolution of
90              the path argument.
91
92       EMFILE All  file  descriptors  available  to  the process are currently
93              open.
94
95       ENAMETOOLONG
96              The  length  of  a  component  of  a  pathname  is  longer  than
97              {NAME_MAX}.
98
99       ENFILE The  maximum  allowable number of files is currently open in the
100              system.
101
102       ENOENT The mode string begins with 'r' and a component of pathname does
103              not  name an existing file, or mode begins with 'w' or 'a' and a
104              component of the path prefix of pathname does not name an exist‐
105              ing file, or pathname is an empty string.
106
107       ENOENT or ENOTDIR
108              The  pathname argument contains at least one non-<slash> charac‐
109              ter and ends with one or more trailing  <slash>  characters.  If
110              pathname  without  the trailing <slash> characters would name an
111              existing file, an [ENOENT] error shall not occur.
112
113       ENOSPC The directory or file system that would  contain  the  new  file
114              cannot  be  expanded,  the file does not exist, and it was to be
115              created.
116
117       ENOTDIR
118              A component of the path prefix names an existing  file  that  is
119              neither  a  directory nor a symbolic link to a directory, or the
120              pathname argument contains at least  one  non-<slash>  character
121              and  ends  with  one or more trailing <slash> characters and the
122              last pathname component names an existing file that is neither a
123              directory nor a symbolic link to a directory.
124
125       ENXIO  The named file is a character special or block special file, and
126              the device associated with this special file does not exist.
127
128       EOVERFLOW
129              The named file is a regular file and the size of the file cannot
130              be represented correctly in an object of type off_t.
131
132       EROFS  The  named  file  resides  on  a  read-only file system and mode
133              requires write access.
134
135       The freopen() function may fail if:
136
137       EBADF  The mode with which the file descriptor  underlying  the  stream
138              was  opened does not support the requested mode when pathname is
139              a null pointer.
140
141       EINVAL The value of the mode argument is not valid.
142
143       ELOOP  More than {SYMLOOP_MAX} symbolic links were  encountered  during
144              resolution of the path argument.
145
146       ENAMETOOLONG
147              The length of a pathname exceeds {PATH_MAX}, or pathname resolu‐
148              tion of a symbolic link produced an intermediate result  with  a
149              length that exceeds {PATH_MAX}.
150
151       ENOMEM Insufficient storage space is available.
152
153       ENXIO  A  request  was made of a nonexistent device, or the request was
154              outside the capabilities of the device.
155
156       ETXTBSY
157              The file is a pure procedure (shared text) file  that  is  being
158              executed and mode requires write access.
159
160       The following sections are informative.
161

EXAMPLES

163   Directing Standard Output to a File
164       The  following  example  logs  all  standard output to the /tmp/logfile
165       file.
166
167
168           #include <stdio.h>
169           ...
170           FILE *fp;
171           ...
172           fp = freopen ("/tmp/logfile", "a+", stdout);
173           ...
174

APPLICATION USAGE

176       The freopen() function is  typically  used  to  attach  the  pre-opened
177       streams associated with stdin, stdout, and stderr to other files.
178
179       Since  implementations  are  not  required  to  support any stream mode
180       changes when the pathname argument is NULL, portable applications  can‐
181       not  rely on the use of freopen() to change the stream mode, and use of
182       this feature is discouraged. The feature was originally  added  to  the
183       ISO C  standard  in  order  to  facilitate changing stdin and stdout to
184       binary mode. Since a 'b' character in the mode has no effect  on  POSIX
185       systems,  this use of the feature is unnecessary in POSIX applications.
186       However, even though the 'b' is ignored,  a  successful  call  to  fre‐
187       open(NULL,  "wb", stdout) does have an effect. In particular, for regu‐
188       lar files it truncates the file and sets  the  file-position  indicator
189       for  the  stream  to  the  start of the file. It is possible that these
190       side-effects are an unintended consequence of the way  the  feature  is
191       specified  in  the  ISO/IEC 9899:1999 standard, but unless or until the
192       ISO C standard is changed, applications which  successfully  call  fre‐
193       open(NULL,  "wb",  stdout) will behave in unexpected ways on conforming
194       systems in situations such as:
195
196
197           { appl file1; appl file2; } > file3
198
199       which will result in file3 containing only the output from  the  second
200       invocation of appl.
201

RATIONALE

203       None.
204

FUTURE DIRECTIONS

206       None.
207

SEE ALSO

209       Section 2.5, Standard I/O Streams, fclose(), fdopen(), fflush(), fmemo‐
210       pen(), fopen(), mbsinit(), open(), open_memstream()
211
212       The Base Definitions volume of POSIX.1‐2017, <stdio.h>
213
215       Portions of this text are reprinted and reproduced in  electronic  form
216       from  IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
217       table Operating System Interface (POSIX), The Open Group Base  Specifi‐
218       cations  Issue  7, 2018 Edition, Copyright (C) 2018 by the Institute of
219       Electrical and Electronics Engineers, Inc and The Open Group.   In  the
220       event of any discrepancy between this version and the original IEEE and
221       The Open Group Standard, the original IEEE and The Open Group  Standard
222       is  the  referee document. The original Standard can be obtained online
223       at http://www.opengroup.org/unix/online.html .
224
225       Any typographical or formatting errors that appear  in  this  page  are
226       most likely to have been introduced during the conversion of the source
227       files to man page format. To report such errors,  see  https://www.ker
228       nel.org/doc/man-pages/reporting_bugs.html .
229
230
231
232IEEE/The Open Group                  2017                          FREOPEN(3P)
Impressum