1FREOPEN(3P) POSIX Programmer's Manual FREOPEN(3P)
2
3
4
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
11
13 freopen — open a stream
14
16 #include <stdio.h>
17
18 FILE *freopen(const char *restrict pathname, const char *restrict mode,
19 FILE *restrict stream);
20
22 The functionality described on this reference page is aligned with the
23 ISO C standard. Any conflict between the requirements described here
24 and the ISO C standard is unintentional. This volume of POSIX.1‐2008
25 defers to the ISO C standard.
26
27 The freopen() function shall first attempt to flush the stream associ‐
28 ated with stream as if by a call to fflush(stream). Failure to flush
29 the stream successfully shall be ignored. If pathname is not a null
30 pointer, freopen() shall close any file descriptor associated with
31 stream. Failure to close the file descriptor successfully shall be
32 ignored. The error and end-of-file indicators for the stream shall be
33 cleared.
34
35 The freopen() function shall open the file whose pathname is the string
36 pointed to by pathname and associate the stream pointed to by stream
37 with it. The mode argument shall be used just as in fopen().
38
39 The original stream shall be closed regardless of whether the subse‐
40 quent open succeeds.
41
42 If pathname is a null pointer, the freopen() function shall attempt to
43 change the mode of the stream to that specified by mode, as if the name
44 of the file currently associated with the stream had been used. In this
45 case, the file descriptor associated with the stream need not be closed
46 if the call to freopen() succeeds. It is implementation-defined which
47 changes of mode are permitted (if any), and under what circumstances.
48
49 After a successful call to the freopen() function, the orientation of
50 the stream shall be cleared, the encoding rule shall be cleared, and
51 the associated mbstate_t object shall be set to describe an initial
52 conversion state.
53
54 If pathname is not a null pointer, or if pathname is a null pointer and
55 the specified mode change necessitates the file descriptor associated
56 with the stream to be closed and reopened, the file descriptor associ‐
57 ated with the reopened stream shall be allocated and opened as if by a
58 call to open() with the following flags:
59
60 ┌─────────────────┬───────────────────────────┐
61 │ freopen() Mode │ open() Flags │
62 ├─────────────────┼───────────────────────────┤
63 │r or rb │ O_RDONLY │
64 │w or wb │ O_WRONLY|O_CREAT|O_TRUNC │
65 │a or ab │ O_WRONLY|O_CREAT|O_APPEND │
66 │r+ or rb+ or r+b │ O_RDWR │
67 │w+ or wb+ or w+b │ O_RDWR|O_CREAT|O_TRUNC │
68 │a+ or ab+ or a+b │ O_RDWR|O_CREAT|O_APPEND │
69 └─────────────────┴───────────────────────────┘
71 Upon successful completion, freopen() shall return the value of stream.
72 Otherwise, a null pointer shall be returned, and errno shall be set to
73 indicate the error.
74
76 The freopen() function shall fail if:
77
78 EACCES Search permission is denied on a component of the path prefix,
79 or the file exists and the permissions specified by mode are
80 denied, or the file does not exist and write permission is
81 denied for the parent directory of the file to be created.
82
83 EBADF The file descriptor underlying the stream is not a valid file
84 descriptor when pathname is a null pointer.
85
86 EINTR A signal was caught during freopen().
87
88 EISDIR The named file is a directory and mode requires write access.
89
90 ELOOP A loop exists in symbolic links encountered during resolution of
91 the path argument.
92
93 EMFILE All file descriptors available to the process are currently
94 open.
95
96 ENAMETOOLONG
97 The length of a component of a pathname is longer than
98 {NAME_MAX}.
99
100 ENFILE The maximum allowable number of files is currently open in the
101 system.
102
103 ENOENT The mode string begins with 'r' and a component of pathname does
104 not name an existing file, or mode begins with 'w' or 'a' and a
105 component of the path prefix of pathname does not name an exist‐
106 ing file, or pathname is an empty string.
107
108 ENOENT or ENOTDIR
109 The pathname argument contains at least one non-<slash> charac‐
110 ter and ends with one or more trailing <slash> characters. If
111 pathname names an existing file, an [ENOENT] error shall not
112 occur.
113
114 ENOSPC The directory or file system that would contain the new file
115 cannot be expanded, the file does not exist, and it was to be
116 created.
117
118 ENOTDIR
119 A component of the path prefix names an existing file that is
120 neither a directory nor a symbolic link to a directory, or the
121 pathname argument contains at least one non-<slash> character
122 and ends with one or more trailing <slash> characters and the
123 last pathname component names an existing file that is neither a
124 directory nor a symbolic link to a directory.
125
126 ENXIO The named file is a character special or block special file, and
127 the device associated with this special file does not exist.
128
129 EOVERFLOW
130 The named file is a regular file and the size of the file cannot
131 be represented correctly in an object of type off_t.
132
133 EROFS The named file resides on a read-only file system and mode
134 requires write access.
135
136 The freopen() function may fail if:
137
138 EBADF The mode with which the file descriptor underlying the stream
139 was opened does not support the requested mode when pathname is
140 a null pointer.
141
142 EINVAL The value of the mode argument is not valid.
143
144 ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during
145 resolution of the path argument.
146
147 ENAMETOOLONG
148 The length of a pathname exceeds {PATH_MAX}, or pathname resolu‐
149 tion of a symbolic link produced an intermediate result with a
150 length that exceeds {PATH_MAX}.
151
152 ENOMEM Insufficient storage space is available.
153
154 ENXIO A request was made of a nonexistent device, or the request was
155 outside the capabilities of the device.
156
157 ETXTBSY
158 The file is a pure procedure (shared text) file that is being
159 executed and mode requires write access.
160
161 The following sections are informative.
162
164 Directing Standard Output to a File
165 The following example logs all standard output to the /tmp/logfile
166 file.
167
168 #include <stdio.h>
169 ...
170 FILE *fp;
171 ...
172 fp = freopen ("/tmp/logfile", "a+", stdout);
173 ...
174
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 { appl file1; appl file2; } > file3
197
198 which will result in file3 containing only the output from the second
199 invocation of appl.
200
202 None.
203
205 None.
206
208 Section 2.5, Standard I/O Streams, fclose(), fdopen(), fflush(), fmemo‐
209 pen(), fopen(), mbsinit(), open(), open_memstream()
210
211 The Base Definitions volume of POSIX.1‐2008, <stdio.h>
212
214 Portions of this text are reprinted and reproduced in electronic form
215 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
216 -- Portable Operating System Interface (POSIX), The Open Group Base
217 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
218 cal and Electronics Engineers, Inc and The Open Group. (This is
219 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) 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.unix.org/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 2013 FREOPEN(3P)