1 .\" Copyright (c) 1990, 1991 The Regents of the University of California.
2 .\" All rights reserved.
4 .\" This code is derived from software contributed to Berkeley by
5 .\" Chris Torek and the American National Standards Committee X3,
6 .\" on Information Processing Systems.
8 .\" SPDX-License-Identifier: BSD-4-Clause-UC
10 .\" @(#)fopen.3 6.8 (Berkeley) 6/29/91
12 .\" Converted for Linux, Mon Nov 29 15:22:01 1993, faith@cs.unc.edu
13 .\" Modified, aeb, 960421, 970806
14 .\" Modified, joey, aeb, 2002-01-03
16 .TH FOPEN 3 2021-03-22 "Linux man-pages (unreleased)"
18 fopen, fdopen, freopen \- stream open functions
21 .RI ( libc ", " \-lc )
26 .BI "FILE *fopen(const char *restrict " pathname \
27 ", const char *restrict " mode );
28 .BI "FILE *fdopen(int " fd ", const char *" mode );
29 .BI "FILE *freopen(const char *restrict " pathname \
30 ", const char *restrict " mode ,
31 .BI " FILE *restrict " stream );
35 Feature Test Macro Requirements for glibc (see
36 .BR feature_test_macros (7)):
46 function opens the file whose name is the string pointed to by
48 and associates a stream with it.
52 points to a string beginning with one of the following sequences
53 (possibly followed by additional characters, as described below):
56 Open text file for reading.
57 The stream is positioned at the beginning of the file.
60 Open for reading and writing.
61 The stream is positioned at the beginning of the file.
64 Truncate file to zero length or create text file for writing.
65 The stream is positioned at the beginning of the file.
68 Open for reading and writing.
69 The file is created if it does not exist, otherwise it is truncated.
70 The stream is positioned at the beginning of
74 Open for appending (writing at end of file).
75 The file is created if it does not exist.
76 The stream is positioned at the end of the file.
79 Open for reading and appending (writing at end of file).
80 The file is created if it does not exist.
81 Output is always appended to the end of the file.
82 POSIX is silent on what the initial read position is when using this mode.
83 For glibc, the initial file position for reading is at
84 the beginning of the file, but for Android/BSD/MacOS, the
85 initial file position for reading is at the end of the file.
89 string can also include the letter \(aqb\(aq either as a last character or as
90 a character between the characters in any of the two-character strings
92 This is strictly for compatibility with C89
93 and has no effect; the \(aqb\(aq is ignored on all POSIX
94 conforming systems, including Linux.
95 (Other systems may treat text files and binary files differently,
96 and adding the \(aqb\(aq may be a good idea if you do I/O to a binary
97 file and expect that your program may be ported to non-UNIX
100 See NOTES below for details of glibc extensions for
103 Any created file will have the mode
104 .BR S_IRUSR " | " S_IWUSR " | " S_IRGRP " | " S_IWGRP " | " S_IROTH " | " S_IWOTH
105 (0666), as modified by the process's umask value (see
108 Reads and writes may be intermixed on read/write streams in any order.
109 Note that ANSI C requires that a file positioning function intervene
110 between output and input, unless an input operation encounters end-of-file.
111 (If this condition is not met, then a read is allowed to return the
112 result of writes other than the most recent.)
113 Therefore it is good practice (and indeed sometimes necessary
114 under Linux) to put an
118 operation between write and read operations on such a stream.
119 This operation may be an apparent no-op
120 (as in \fIfseek(..., 0L, SEEK_CUR)\fP
121 called for its synchronizing side effect).
123 Opening a file in append mode (\fBa\fP as the first character of
125 causes all subsequent write operations to this stream to occur
126 at end-of-file, as if preceded the call:
130 fseek(stream, 0, SEEK_END);
134 The file descriptor associated with the stream is opened as if by a call to
136 with the following flags:
142 fopen() mode open() flags
144 \fIw\fP O_WRONLY | O_CREAT | O_TRUNC
145 \fIa\fP O_WRONLY | O_CREAT | O_APPEND
147 \fIw+\fP O_RDWR | O_CREAT | O_TRUNC
148 \fIa+\fP O_RDWR | O_CREAT | O_APPEND
155 function associates a stream with the existing file descriptor,
159 of the stream (one of the values "r", "r+", "w", "w+", "a", "a+")
160 must be compatible with the mode of the file descriptor.
161 The file position indicator of the new stream is set to that
164 and the error and end-of-file indicators are cleared.
165 Modes "w" or "w+" do not cause truncation of the file.
166 The file descriptor is not dup'ed, and will be closed when
167 the stream created by
170 The result of applying
172 to a shared memory object is undefined.
177 function opens the file whose name is the string pointed to by
179 and associates the stream pointed to by
182 The original stream (if it exists) is closed.
185 argument is used just as in the
191 argument is a null pointer,
193 changes the mode of the stream to that specified in
197 reopens the pathname that is associated with the stream.
198 The specification for this behavior was added in the C99 standard, which says:
202 the file descriptor associated with the stream need not be closed
206 It is implementation-defined which changes of mode are permitted (if any),
207 and under what circumstances.
210 The primary use of the
212 function is to change the file associated with a standard text stream
213 .RI ( stderr ", " stdin ", or " stdout ).
215 Upon successful completion
223 Otherwise, NULL is returned and
225 is set to indicate the error.
243 functions may also fail and set
245 for any of the errors specified for the routine
250 function may also fail and set
252 for any of the errors specified for the routine
257 function may also fail and set
259 for any of the errors specified for the routine
264 function may also fail and set
266 for any of the errors specified for the routines
272 For an explanation of the terms used in this section, see
280 Interface Attribute Value
285 T} Thread safety MT-Safe
293 POSIX.1-2001, POSIX.1-2008, C89, C99.
296 POSIX.1-2001, POSIX.1-2008.
299 The GNU C library allows the following extensions for the string specified in
302 .BR c " (since glibc 2.3.3)"
303 Do not make the open operation,
304 or subsequent read and write operations,
305 thread cancelation points.
306 This flag is ignored for
309 .BR e " (since glibc 2.7)"
310 Open the file with the
315 for more information.
316 This flag is ignored for
319 .BR m " (since glibc 2.3)"
320 Attempt to access the file using
322 rather than I/O system calls
329 is attempted only for a file opened for reading.
333 .\" FIXME . C11 specifies this flag
334 Open the file exclusively
339 If the file already exists,
345 This flag is ignored for
348 In addition to the above characters,
352 support the following syntax
360 is taken as the name of a coded character set and
361 the stream is marked as wide-oriented.
362 Thereafter, internal conversion functions convert I/O
363 to and from the character set
367 syntax is not specified,
368 then the wide-orientation of the stream is
369 determined by the first file operation.
370 If that operation is a wide-character operation,
371 the stream is marked wide-oriented,
372 and functions to convert to the coded character set are loaded.
374 When parsing for individual flag characters in
376 (i.e., the characters preceding the "ccs" specification),
377 the glibc implementation of
378 .\" FIXME . http://sourceware.org/bugzilla/show_bug.cgi?id=12685
382 limits the number of characters examined in
384 to 7 (or, in glibc versions before 2.14, to 6,
385 which was not enough to include possible specifications such as "rb+cmxe").
386 The current implementation of
388 parses at most 5 characters in
396 .BR open_memstream (3)