1FOPEN(3) Linux Programmer's Manual FOPEN(3)
2
3
4
6 fopen, fdopen, freopen - stream open functions
7
9 #include <stdio.h>
10
11 FILE *fopen(const char *restrict pathname, const char *restrict mode);
12 FILE *fdopen(int fd, const char *mode);
13 FILE *freopen(const char *restrict pathname, const char *restrict mode,
14 FILE *restrict stream);
15
16 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
17
18 fdopen():
19 _POSIX_C_SOURCE
20
22 The fopen() function opens the file whose name is the string pointed to
23 by pathname and associates a stream with it.
24
25 The argument mode points to a string beginning with one of the follow‐
26 ing sequences (possibly followed by additional characters, as described
27 below):
28
29 r Open text file for reading. The stream is positioned at the be‐
30 ginning of the file.
31
32 r+ Open for reading and writing. The stream is positioned at the
33 beginning of the file.
34
35 w Truncate file to zero length or create text file for writing.
36 The stream is positioned at the beginning of the file.
37
38 w+ Open for reading and writing. The file is created if it does
39 not exist, otherwise it is truncated. The stream is positioned
40 at the beginning of the file.
41
42 a Open for appending (writing at end of file). The file is cre‐
43 ated if it does not exist. The stream is positioned at the end
44 of the file.
45
46 a+ Open for reading and appending (writing at end of file). The
47 file is created if it does not exist. Output is always appended
48 to the end of the file. POSIX is silent on what the initial
49 read position is when using this mode. For glibc, the initial
50 file position for reading is at the beginning of the file, but
51 for Android/BSD/MacOS, the initial file position for reading is
52 at the end of the file.
53
54 The mode string can also include the letter 'b' either as a last char‐
55 acter or as a character between the characters in any of the two-char‐
56 acter strings described above. This is strictly for compatibility with
57 C89 and has no effect; the 'b' is ignored on all POSIX conforming sys‐
58 tems, including Linux. (Other systems may treat text files and binary
59 files differently, and adding the 'b' may be a good idea if you do I/O
60 to a binary file and expect that your program may be ported to non-UNIX
61 environments.)
62
63 See NOTES below for details of glibc extensions for mode.
64
65 Any created file will have the mode S_IRUSR | S_IWUSR | S_IRGRP | S_IW‐
66 GRP | S_IROTH | S_IWOTH [22m(0666), as modified by the process's umask
67 value (see umask(2)).
68
69 Reads and writes may be intermixed on read/write streams in any order.
70 Note that ANSI C requires that a file positioning function intervene
71 between output and input, unless an input operation encounters end-of-
72 file. (If this condition is not met, then a read is allowed to return
73 the result of writes other than the most recent.) Therefore it is good
74 practice (and indeed sometimes necessary under Linux) to put an
75 fseek(3) or fgetpos(3) operation between write and read operations on
76 such a stream. This operation may be an apparent no-op (as in
77 fseek(..., 0L, SEEK_CUR) called for its synchronizing side effect).
78
79 Opening a file in append mode (a as the first character of mode) causes
80 all subsequent write operations to this stream to occur at end-of-file,
81 as if preceded the call:
82
83 fseek(stream, 0, SEEK_END);
84
85 The file descriptor associated with the stream is opened as if by a
86 call to open(2) with the following flags:
87
88 ┌─────────────┬───────────────────────────────┐
89 │fopen() mode │ open() flags │
90 ├─────────────┼───────────────────────────────┤
91 │ r │ O_RDONLY │
92 ├─────────────┼───────────────────────────────┤
93 │ w │ O_WRONLY | O_CREAT | O_TRUNC │
94 ├─────────────┼───────────────────────────────┤
95 │ a │ O_WRONLY | O_CREAT | O_APPEND │
96 ├─────────────┼───────────────────────────────┤
97 │ r+ │ O_RDWR │
98 ├─────────────┼───────────────────────────────┤
99 │ w+ │ O_RDWR | O_CREAT | O_TRUNC │
100 ├─────────────┼───────────────────────────────┤
101 │ a+ │ O_RDWR | O_CREAT | O_APPEND │
102 └─────────────┴───────────────────────────────┘
103 fdopen()
104 The fdopen() function associates a stream with the existing file de‐
105 scriptor, fd. The mode of the stream (one of the values "r", "r+",
106 "w", "w+", "a", "a+") must be compatible with the mode of the file de‐
107 scriptor. The file position indicator of the new stream is set to that
108 belonging to fd, and the error and end-of-file indicators are cleared.
109 Modes "w" or "w+" do not cause truncation of the file. The file de‐
110 scriptor is not dup'ed, and will be closed when the stream created by
111 fdopen() is closed. The result of applying fdopen() to a shared memory
112 object is undefined.
113
114 freopen()
115 The freopen() function opens the file whose name is the string pointed
116 to by pathname and associates the stream pointed to by stream with it.
117 The original stream (if it exists) is closed. The mode argument is
118 used just as in the fopen() function.
119
120 If the pathname argument is a null pointer, freopen() changes the mode
121 of the stream to that specified in mode; that is, freopen() reopens the
122 pathname that is associated with the stream. The specification for
123 this behavior was added in the C99 standard, which says:
124
125 In this case, the file descriptor associated with the stream
126 need not be closed if the call to freopen() succeeds. It is im‐
127 plementation-defined which changes of mode are permitted (if
128 any), and under what circumstances.
129
130 The primary use of the freopen() function is to change the file associ‐
131 ated with a standard text stream (stderr, stdin, or stdout).
132
134 Upon successful completion fopen(), fdopen(), and freopen() return a
135 FILE pointer. Otherwise, NULL is returned and errno is set to indicate
136 the error.
137
139 EINVAL The mode provided to fopen(), fdopen(), or freopen() was in‐
140 valid.
141
142 The fopen(), fdopen(), and freopen() functions may also fail and set
143 errno for any of the errors specified for the routine malloc(3).
144
145 The fopen() function may also fail and set errno for any of the errors
146 specified for the routine open(2).
147
148 The fdopen() function may also fail and set errno for any of the errors
149 specified for the routine fcntl(2).
150
151 The freopen() function may also fail and set errno for any of the er‐
152 rors specified for the routines open(2), fclose(3), and fflush(3).
153
155 For an explanation of the terms used in this section, see at‐
156 tributes(7).
157
158 ┌────────────────────────────────────────────┬───────────────┬─────────┐
159 │Interface │ Attribute │ Value │
160 ├────────────────────────────────────────────┼───────────────┼─────────┤
161 │fopen(), fdopen(), freopen() │ Thread safety │ MT-Safe │
162 └────────────────────────────────────────────┴───────────────┴─────────┘
163
165 fopen(), freopen(): POSIX.1-2001, POSIX.1-2008, C89, C99.
166
167 fdopen(): POSIX.1-2001, POSIX.1-2008.
168
170 Glibc notes
171 The GNU C library allows the following extensions for the string speci‐
172 fied in mode:
173
174 c (since glibc 2.3.3)
175 Do not make the open operation, or subsequent read and write op‐
176 erations, thread cancellation points. This flag is ignored for
177 fdopen().
178
179 e (since glibc 2.7)
180 Open the file with the O_CLOEXEC flag. See open(2) for more in‐
181 formation. This flag is ignored for fdopen().
182
183 m (since glibc 2.3)
184 Attempt to access the file using mmap(2), rather than I/O system
185 calls (read(2), write(2)). Currently, use of mmap(2) is at‐
186 tempted only for a file opened for reading.
187
188 x Open the file exclusively (like the O_EXCL flag of open(2)). If
189 the file already exists, fopen() fails, and sets errno to EEX‐
190 IST. This flag is ignored for fdopen().
191
192 In addition to the above characters, fopen() and freopen() support the
193 following syntax in mode:
194
195 ,ccs=string
196
197 The given string is taken as the name of a coded character set and the
198 stream is marked as wide-oriented. Thereafter, internal conversion
199 functions convert I/O to and from the character set string. If the
200 ,ccs=string syntax is not specified, then the wide-orientation of the
201 stream is determined by the first file operation. If that operation is
202 a wide-character operation, the stream is marked wide-oriented, and
203 functions to convert to the coded character set are loaded.
204
206 When parsing for individual flag characters in mode (i.e., the charac‐
207 ters preceding the "ccs" specification), the glibc implementation of
208 fopen() and freopen() limits the number of characters examined in mode
209 to 7 (or, in glibc versions before 2.14, to 6, which was not enough to
210 include possible specifications such as "rb+cmxe"). The current imple‐
211 mentation of fdopen() parses at most 5 characters in mode.
212
214 open(2), fclose(3), fileno(3), fmemopen(3), fopencookie(3), open_mem‐
215 stream(3)
216
218 This page is part of release 5.13 of the Linux man-pages project. A
219 description of the project, information about reporting bugs, and the
220 latest version of this page, can be found at
221 https://www.kernel.org/doc/man-pages/.
222
223
224
225GNU 2021-03-22 FOPEN(3)