Debian Codegraph @

upstream/debian-trixie/man3/fopen.3

raw
1 '\" t
2 .\" Copyright (c) 1990, 1991 The Regents of the University of California.
3 .\" All rights reserved.
4 .\"
5 .\" This code is derived from software contributed to Berkeley by
6 .\" Chris Torek and the American National Standards Committee X3,
7 .\" on Information Processing Systems.
8 .\"
9 .\" SPDX-License-Identifier: BSD-4-Clause-UC
10 .\"
11 .\" @(#)fopen.3 6.8 (Berkeley) 6/29/91
12 .\"
13 .\" Converted for Linux, Mon Nov 29 15:22:01 1993, [email protected]
14 .\" Modified, aeb, 960421, 970806
15 .\" Modified, joey, aeb, 2002-01-03
16 .\"
17 .TH fopen 3 2024-05-02 "Linux man-pages 6.9.1"
18 .SH NAME
19 fopen, fdopen, freopen \- stream open functions
20 .SH LIBRARY
21 Standard C library
22 .RI ( libc ", " \-lc )
23 .SH SYNOPSIS
24 .nf
25 .B #include <stdio.h>
26 .P
27 .BI "FILE *fopen(const char *restrict " pathname \
28 ", const char *restrict " mode );
29 .BI "FILE *fdopen(int " fd ", const char *" mode );
30 .BI "FILE *freopen(const char *restrict " pathname \
31 ", const char *restrict " mode ,
32 .BI " FILE *restrict " stream );
33 .fi
34 .P
35 .RS -4
36 Feature Test Macro Requirements for glibc (see
37 .BR feature_test_macros (7)):
38 .RE
39 .P
40 .BR fdopen ():
41 .nf
42 _POSIX_C_SOURCE
43 .fi
44 .SH DESCRIPTION
45 The
46 .BR fopen ()
47 function opens the file whose name is the string pointed to by
48 .I pathname
49 and associates a stream with it.
50 .P
51 The argument
52 .I mode
53 points to a string beginning with one of the following sequences
54 (possibly followed by additional characters, as described below):
55 .TP
56 .B r
57 Open text file for reading.
58 The stream is positioned at the beginning of the file.
59 .TP
60 .B r+
61 Open for reading and writing.
62 The stream is positioned at the beginning of the file.
63 .TP
64 .B w
65 Truncate file to zero length or create text file for writing.
66 The stream is positioned at the beginning of the file.
67 .TP
68 .B w+
69 Open for reading and writing.
70 The file is created if it does not exist, otherwise it is truncated.
71 The stream is positioned at the beginning of
72 the file.
73 .TP
74 .B a
75 Open for appending (writing at end of file).
76 The file is created if it does not exist.
77 The stream is positioned at the end of the file.
78 .TP
79 .B a+
80 Open for reading and appending (writing at end of file).
81 The file is created if it does not exist.
82 Output is always appended to the end of the file.
83 POSIX is silent on what the initial read position is when using this mode.
84 For glibc, the initial file position for reading is at
85 the beginning of the file, but for Android/BSD/MacOS, the
86 initial file position for reading is at the end of the file.
87 .P
88 The
89 .I mode
90 string can also include the letter \[aq]b\[aq] either as a last character or as
91 a character between the characters in any of the two-character strings
92 described above.
93 This is strictly for compatibility with ISO C
94 and has no effect; the \[aq]b\[aq] is ignored on all POSIX
95 conforming systems, including Linux.
96 (Other systems may treat text files and binary files differently,
97 and adding the \[aq]b\[aq] may be a good idea if you do I/O to a binary
98 file and expect that your program may be ported to non-UNIX
99 environments.)
100 .P
101 See NOTES below for details of glibc extensions for
102 .IR mode .
103 .P
104 Any created file will have the mode
105 .BR S_IRUSR " | " S_IWUSR " | " S_IRGRP " | " S_IWGRP " | " S_IROTH " | " S_IWOTH
106 (0666), as modified by the process's umask value (see
107 .BR umask (2)).
108 .P
109 Reads and writes may be intermixed on read/write streams in any order.
110 Note that ANSI C requires that a file positioning function intervene
111 between output and input, unless an input operation encounters end-of-file.
112 (If this condition is not met, then a read is allowed to return the
113 result of writes other than the most recent.)
114 Therefore it is good practice (and indeed sometimes necessary
115 under Linux) to put an
116 .BR fseek (3)
117 or
118 .BR fsetpos (3)
119 operation between write and read operations on such a stream.
120 This operation may be an apparent no-op
121 (as in \fIfseek(..., 0L, SEEK_CUR)\fP
122 called for its synchronizing side effect).
123 .P
124 Opening a file in append mode (\fBa\fP as the first character of
125 .IR mode )
126 causes all subsequent write operations to this stream to occur
127 at end-of-file, as if preceded by the call:
128 .P
129 .in +4n
130 .EX
131 fseek(stream, 0, SEEK_END);
132 .EE
133 .in
134 .P
135 The file descriptor associated with the stream is opened as if by a call to
136 .BR open (2)
137 with the following flags:
138 .RS
139 .TS
140 allbox;
141 lb lb
142 c l.
143 fopen() mode open() flags
144 \fIr\fP O_RDONLY
145 \fIw\fP O_WRONLY | O_CREAT | O_TRUNC
146 \fIa\fP O_WRONLY | O_CREAT | O_APPEND
147 \fIr+\fP O_RDWR
148 \fIw+\fP O_RDWR | O_CREAT | O_TRUNC
149 \fIa+\fP O_RDWR | O_CREAT | O_APPEND
150 .TE
151 .RE
152 .\"
153 .SS fdopen()
154 The
155 .BR fdopen ()
156 function associates a stream with the existing file descriptor,
157 .IR fd .
158 The
159 .I mode
160 of the stream (one of the values "r", "r+", "w", "w+", "a", "a+")
161 must be compatible with the mode of the file descriptor.
162 The file position indicator of the new stream is set to that
163 belonging to
164 .IR fd ,
165 and the error and end-of-file indicators are cleared.
166 Modes "w" or "w+" do not cause truncation of the file.
167 The file descriptor is not dup'ed, and will be closed when
168 the stream created by
169 .BR fdopen ()
170 is closed.
171 The result of applying
172 .BR fdopen ()
173 to a shared memory object is undefined.
174 .\"
175 .SS freopen()
176 The
177 .BR freopen ()
178 function opens the file whose name is the string pointed to by
179 .I pathname
180 and associates the stream pointed to by
181 .I stream
182 with it.
183 The original stream (if it exists) is closed.
184 The
185 .I mode
186 argument is used just as in the
187 .BR fopen ()
188 function.
189 .P
190 If the
191 .I pathname
192 argument is a null pointer,
193 .BR freopen ()
194 changes the mode of the stream to that specified in
195 .IR mode ;
196 that is,
197 .BR freopen ()
198 reopens the pathname that is associated with the stream.
199 The specification for this behavior was added in the C99 standard, which says:
200 .P
201 .RS
202 In this case,
203 the file descriptor associated with the stream need not be closed
204 if the call to
205 .BR freopen ()
206 succeeds.
207 It is implementation-defined which changes of mode are permitted (if any),
208 and under what circumstances.
209 .RE
210 .P
211 The primary use of the
212 .BR freopen ()
213 function is to change the file associated with a standard text stream
214 .RI ( stderr ", " stdin ", or " stdout ).
215 .SH RETURN VALUE
216 Upon successful completion
217 .BR fopen (),
218 .BR fdopen (),
219 and
220 .BR freopen ()
221 return a
222 .I FILE
223 pointer.
224 Otherwise, NULL is returned and
225 .I errno
226 is set to indicate the error.
227 .SH ERRORS
228 .TP
229 .B EINVAL
230 The
231 .I mode
232 provided to
233 .BR fopen (),
234 .BR fdopen (),
235 or
236 .BR freopen ()
237 was invalid.
238 .P
239 The
240 .BR fopen (),
241 .BR fdopen (),
242 and
243 .BR freopen ()
244 functions may also fail and set
245 .I errno
246 for any of the errors specified for the routine
247 .BR malloc (3).
248 .P
249 The
250 .BR fopen ()
251 function may also fail and set
252 .I errno
253 for any of the errors specified for the routine
254 .BR open (2).
255 .P
256 The
257 .BR fdopen ()
258 function may also fail and set
259 .I errno
260 for any of the errors specified for the routine
261 .BR fcntl (2).
262 .P
263 The
264 .BR freopen ()
265 function may also fail and set
266 .I errno
267 for any of the errors specified for the routines
268 .BR open (2),
269 .BR fclose (3),
270 and
271 .BR fflush (3).
272 .SH ATTRIBUTES
273 For an explanation of the terms used in this section, see
274 .BR attributes (7).
275 .TS
276 allbox;
277 lbx lb lb
278 l l l.
279 Interface Attribute Value
280 T{
281 .na
282 .nh
283 .BR fopen (),
284 .BR fdopen (),
285 .BR freopen ()
286 T} Thread safety MT-Safe
287 .TE
288 .SH STANDARDS
289 .TP
290 .BR fopen ()
291 .TQ
292 .BR freopen ()
293 C11, POSIX.1-2008.
294 .TP
295 .BR fdopen ()
296 POSIX.1-2008.
297 .SH HISTORY
298 .TP
299 .BR fopen ()
300 .TQ
301 .BR freopen ()
302 POSIX.1-2001, C89.
303 .TP
304 .BR fdopen ()
305 POSIX.1-2001.
306 .SH NOTES
307 .SS glibc notes
308 The GNU C library allows the following extensions for the string specified in
309 .IR mode :
310 .TP
311 .BR c " (since glibc 2.3.3)"
312 Do not make the open operation,
313 or subsequent read and write operations,
314 thread cancelation points.
315 This flag is ignored for
316 .BR fdopen ().
317 .TP
318 .BR e " (since glibc 2.7)"
319 Open the file with the
320 .B O_CLOEXEC
321 flag.
322 See
323 .BR open (2)
324 for more information.
325 This flag is ignored for
326 .BR fdopen ().
327 .TP
328 .BR m " (since glibc 2.3)"
329 Attempt to access the file using
330 .BR mmap (2),
331 rather than I/O system calls
332 .RB ( read (2),
333 .BR write (2)).
334 Currently,
335 .\" As at glibc 2.4:
336 use of
337 .BR mmap (2)
338 is attempted only for a file opened for reading.
339 .TP
340 .B x
341 .\" Since glibc 2.0?
342 .\" FIXME . C11 specifies this flag
343 Open the file exclusively
344 (like the
345 .B O_EXCL
346 flag of
347 .BR open (2)).
348 If the file already exists,
349 .BR fopen ()
350 fails, and sets
351 .I errno
352 to
353 .BR EEXIST .
354 This flag is ignored for
355 .BR fdopen ().
356 .P
357 In addition to the above characters,
358 .BR fopen ()
359 and
360 .BR freopen ()
361 support the following syntax
362 in
363 .IR mode :
364 .P
365 .BI " ,ccs=" string
366 .P
367 The given
368 .I string
369 is taken as the name of a coded character set and
370 the stream is marked as wide-oriented.
371 Thereafter, internal conversion functions convert I/O
372 to and from the character set
373 .IR string .
374 If the
375 .BI ,ccs= string
376 syntax is not specified,
377 then the wide-orientation of the stream is
378 determined by the first file operation.
379 If that operation is a wide-character operation,
380 the stream is marked wide-oriented,
381 and functions to convert to the coded character set are loaded.
382 .SH BUGS
383 When parsing for individual flag characters in
384 .I mode
385 (i.e., the characters preceding the "ccs" specification),
386 the glibc implementation of
387 .\" FIXME . http://sourceware.org/bugzilla/show_bug.cgi?id=12685
388 .BR fopen ()
389 and
390 .BR freopen ()
391 limits the number of characters examined in
392 .I mode
393 to 7 (or, before glibc 2.14, to 6,
394 which was not enough to include possible specifications such as "rb+cmxe").
395 The current implementation of
396 .BR fdopen ()
397 parses at most 5 characters in
398 .IR mode .
399 .SH SEE ALSO
400 .BR open (2),
401 .BR fclose (3),
402 .BR fileno (3),
403 .BR fmemopen (3),
404 .BR fopencookie (3),
405 .BR open_memstream (3)