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 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
16 .\" 3. All advertising materials mentioning features or use of this software
17 .\" must display the following acknowledgement:
18 .\" This product includes software developed by the University of
19 .\" California, Berkeley and its contributors.
20 .\" 4. Neither the name of the University nor the names of its contributors
21 .\" may be used to endorse or promote products derived from this software
22 .\" without specific prior written permission.
24 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .\" @(#)fopen.3 6.8 (Berkeley) 6/29/91
38 .\" Converted for Linux, Mon Nov 29 15:22:01 1993, faith@cs.unc.edu
39 .\" Modified, aeb, 960421, 970806
40 .\" Modified, joey, aeb, 2002-01-03
42 .TH FOPEN 3 2009-02-23 "GNU" "Linux Programmer's Manual"
44 fopen, fdopen, freopen \- stream open functions
49 .BI "FILE *fopen(const char *" path ", const char *" mode );
51 .BI "FILE *fdopen(int " fd ", const char *" mode );
53 .BI "FILE *freopen(const char *" path ", const char *" mode ", FILE *" stream );
57 Feature Test Macro Requirements for glibc (see
58 .BR feature_test_macros (7)):
62 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
66 function opens the file whose name is the string pointed to by
68 and associates a stream with it.
72 points to a string beginning with one of the following sequences
73 (Additional characters may follow these sequences.):
76 Open text file for reading.
77 The stream is positioned at the beginning of the file.
80 Open for reading and writing.
81 The stream is positioned at the beginning of the file.
84 Truncate file to zero length or create text file for writing.
85 The stream is positioned at the beginning of the file.
88 Open for reading and writing.
89 The file is created if it does not exist, otherwise it is truncated.
90 The stream is positioned at the beginning of
94 Open for appending (writing at end of file).
95 The file is created if it does not exist.
96 The stream is positioned at the end of the file.
99 Open for reading and appending (writing at end of file).
100 The file is created if it does not exist.
101 The initial file position for reading is at the beginning of the file,
102 but output is always appended to the end of the file.
106 string can also include the letter \(aqb\(aq either as a last character or as
107 a character between the characters in any of the two-character strings
109 This is strictly for compatibility with C89
110 and has no effect; the \(aqb\(aq is ignored on all POSIX
111 conforming systems, including Linux.
112 (Other systems may treat text files and binary files differently,
113 and adding the \(aqb\(aq may be a good idea if you do I/O to a binary
114 file and expect that your program may be ported to non-UNIX
117 See NOTES below for details of glibc extensions for
120 Any created files will have mode
121 .BR S_IRUSR " | " S_IWUSR " | " S_IRGRP " | " S_IWGRP " | " S_IROTH " | " S_IWOTH
122 (0666), as modified by the process's umask value (see
125 Reads and writes may be intermixed on read/write streams in any order.
126 Note that ANSI C requires that a file positioning function intervene
127 between output and input, unless an input operation encounters end-of-file.
128 (If this condition is not met, then a read is allowed to return the
129 result of writes other than the most recent.)
130 Therefore it is good practice (and indeed sometimes necessary
131 under Linux) to put an
135 operation between write and read operations on such a stream.
136 This operation may be an apparent no-op
137 (as in \fIfseek(..., 0L, SEEK_CUR)\fP
138 called for its synchronizing side effect.
140 Opening a file in append mode (\fBa\fP as the first character of
142 causes all subsequent write operations to this stream to occur
143 at end-of-file, as if preceded by an
146 fseek(stream,0,SEEK_END);
153 function associates a stream with the existing file descriptor,
157 of the stream (one of the values "r", "r+", "w", "w+", "a", "a+")
158 must be compatible with the mode of the file descriptor.
159 The file position indicator of the new stream is set to that
162 and the error and end-of-file indicators are cleared.
163 Modes "w" or "w+" do not cause truncation of the file.
164 The file descriptor is not dup'ed, and will be closed when
165 the stream created by
168 The result of applying
170 to a shared memory object is undefined.
174 function opens the file whose name is the string pointed to by
176 and associates the stream pointed to by
179 The original stream (if it exists) is closed.
182 argument is used just as in the
185 The primary use of the
187 function is to change the file associated with a standard text stream
188 .RI ( stderr ", " stdin ", or " stdout ).
190 Upon successful completion
198 Otherwise, NULL is returned and
200 is set to indicate the error.
218 functions may also fail and set
220 for any of the errors specified for the routine
225 function may also fail and set
227 for any of the errors specified for the routine
232 function may also fail and set
234 for any of the errors specified for the routine
239 function may also fail and set
241 for any of the errors specified for the routines
251 functions conform to C89.
254 function conforms to POSIX.1-1990.
257 The GNU C library allows the following extensions for the string specified in
260 .BR c " (since glibc 2.3.3)"
261 Do not make the open operation,
262 or subsequent read and write operations,
263 thread cancellation points.
265 .BR e " (since glibc 2.7)"
266 Open the file with the
271 for more information.
273 .BR m " (since glibc 2.3)"
274 Attempt to access the file using
276 rather than I/O system calls
283 is only attempted for a file opened for reading.
287 .\" FIXME C11 specifies this flag
288 Open the file exclusively
293 If the file already exists,
299 This flag is ignored for
301 .\" FIXME document /,ccs= charset/