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 *path, const char *mode);
12
13 FILE *fdopen(int fd, const char *mode);
14
15 FILE *freopen(const char *path, const char *mode, FILE *stream);
16
17 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19 fdopen(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
20
22 The fopen() function opens the file whose name is the string pointed to
23 by path 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
30 beginning 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. The initial file position
48 for reading is at the beginning of the file, but output is
49 always appended to the end of the file.
50
51 The mode string can also include the letter 'b' either as a last char‐
52 acter or as a character between the characters in any of the two-char‐
53 acter strings described above. This is strictly for compatibility with
54 C89 and has no effect; the 'b' is ignored on all POSIX conforming sys‐
55 tems, including Linux. (Other systems may treat text files and binary
56 files differently, and adding the 'b' may be a good idea if you do I/O
57 to a binary file and expect that your program may be ported to non-UNIX
58 environments.)
59
60 See NOTES below for details of glibc extensions for mode.
61
62 Any created files will have mode S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
63 | S_IROTH | S_IWOTH (0666), as modified by the process's umask value
64 (see umask(2)).
65
66 Reads and writes may be intermixed on read/write streams in any order.
67 Note that ANSI C requires that a file positioning function intervene
68 between output and input, unless an input operation encounters end-of-
69 file. (If this condition is not met, then a read is allowed to return
70 the result of writes other than the most recent.) Therefore it is good
71 practice (and indeed sometimes necessary under Linux) to put an
72 fseek(3) or fgetpos(3) operation between write and read operations on
73 such a stream. This operation may be an apparent no-op (as in
74 fseek(..., 0L, SEEK_CUR) called for its synchronizing side effect.
75
76 Opening a file in append mode (a as the first character of mode) causes
77 all subsequent write operations to this stream to occur at end-of-file,
78 as if preceded the call:
79
80 fseek(stream, 0, SEEK_END);
81
82 The fdopen() function associates a stream with the existing file
83 descriptor, fd. The mode of the stream (one of the values "r", "r+",
84 "w", "w+", "a", "a+") must be compatible with the mode of the file
85 descriptor. The file position indicator of the new stream is set to
86 that belonging to fd, and the error and end-of-file indicators are
87 cleared. Modes "w" or "w+" do not cause truncation of the file. The
88 file descriptor is not dup'ed, and will be closed when the stream cre‐
89 ated by fdopen() is closed. The result of applying fdopen() to a
90 shared memory object is undefined.
91
92 The freopen() function opens the file whose name is the string pointed
93 to by path and associates the stream pointed to by stream with it. The
94 original stream (if it exists) is closed. The mode argument is used
95 just as in the fopen() function. The primary use of the freopen()
96 function is to change the file associated with a standard text stream
97 (stderr, stdin, or stdout).
98
100 Upon successful completion fopen(), fdopen() and freopen() return a
101 FILE pointer. Otherwise, NULL is returned and errno is set to indicate
102 the error.
103
105 EINVAL The mode provided to fopen(), fdopen(), or freopen() was
106 invalid.
107
108 The fopen(), fdopen() and freopen() functions may also fail and set
109 errno for any of the errors specified for the routine malloc(3).
110
111 The fopen() function may also fail and set errno for any of the errors
112 specified for the routine open(2).
113
114 The fdopen() function may also fail and set errno for any of the errors
115 specified for the routine fcntl(2).
116
117 The freopen() function may also fail and set errno for any of the
118 errors specified for the routines open(2), fclose(3) and fflush(3).
119
121 The fopen() and freopen() functions conform to C89. The fdopen() func‐
122 tion conforms to POSIX.1-1990.
123
125 Glibc notes
126 The GNU C library allows the following extensions for the string speci‐
127 fied in mode:
128
129 c (since glibc 2.3.3)
130 Do not make the open operation, or subsequent read and write
131 operations, thread cancellation points. This flag is ignored
132 for fdopen().
133
134 e (since glibc 2.7)
135 Open the file with the O_CLOEXEC flag. See open(2) for more
136 information. This flag is ignored for fdopen().
137
138 m (since glibc 2.3)
139 Attempt to access the file using mmap(2), rather than I/O system
140 calls (read(2), write(2)). Currently, use of mmap(2) is
141 attempted only for a file opened for reading.
142
143 x Open the file exclusively (like the O_EXCL flag of open(2)). If
144 the file already exists, fopen() fails, and sets errno to EEX‐
145 IST. This flag is ignored for fdopen().
146
147 In addition to the above characters, fopen() and freopen() support the
148 following syntax in mode:
149
150 ,ccs=string
151
152 The given string is taken as the name of a coded character set and the
153 stream is marked as wide-oriented. Thereafter, internal conversion
154 functions convert I/O to and from the character set string. If the
155 ,ccs=string syntax is not specified, then the wide-orientation of the
156 stream is determined by the first file operation. If that operation is
157 a wide-character operation, the stream is marked wide-oriented, and
158 functions to convert to the coded character set are loaded.
159
161 When parsing for individual flag characters in mode (i.e., the charac‐
162 ters preceding the "ccs" specification), the glibc implementation of
163 fopen() and freopen() limits the number of characters examined in mode
164 to 7 (or, in glibc versions before 2.14, to 6, which was not enough to
165 include possible specifications such as "rb+cmxe"). The current imple‐
166 mentation of fdopen() parses at most 5 characters in mode.
167
169 open(2), fclose(3), fileno(3), fmemopen(3), fopencookie(3)
170
172 This page is part of release 3.53 of the Linux man-pages project. A
173 description of the project, and information about reporting bugs, can
174 be found at http://www.kernel.org/doc/man-pages/.
175
176
177
178GNU 2012-04-22 FOPEN(3)