OSDN Git Service

(split) LDP: Update original to LDP v3.40.
[linuxjm/LDP_man-pages.git] / original / man3 / fopen.3
1 .\" Copyright (c) 1990, 1991 The Regents of the University of California.
2 .\" All rights reserved.
3 .\"
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.
7 .\"
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
10 .\" are met:
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.
23 .\"
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
34 .\" SUCH DAMAGE.
35 .\"
36 .\"     @(#)fopen.3     6.8 (Berkeley) 6/29/91
37 .\"
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
41 .\"
42 .TH FOPEN 3  2012-04-22 "GNU" "Linux Programmer's Manual"
43 .SH NAME
44 fopen, fdopen, freopen \- stream open functions
45 .SH SYNOPSIS
46 .nf
47 .B #include <stdio.h>
48 .sp
49 .BI "FILE *fopen(const char *" path ", const char *" mode );
50
51 .BI "FILE *fdopen(int " fd ", const char *" mode );
52
53 .BI "FILE *freopen(const char *" path ", const char *" mode ", FILE *" stream );
54 .fi
55 .sp
56 .in -4n
57 Feature Test Macro Requirements for glibc (see
58 .BR feature_test_macros (7)):
59 .in
60 .sp
61 .BR fdopen ():
62 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
63 .SH DESCRIPTION
64 The
65 .BR fopen ()
66 function opens the file whose name is the string pointed to by
67 .I path
68 and associates a stream with it.
69 .PP
70 The argument
71 .I mode
72 points to a string beginning with one of the following sequences
73 (possibly followed by additional characters, as described below):
74 .TP
75 .B r
76 Open text file for reading.
77 The stream is positioned at the beginning of the file.
78 .TP
79 .B r+
80 Open for reading and writing.
81 The stream is positioned at the beginning of the file.
82 .TP
83 .B w
84 Truncate file to zero length or create text file for writing.
85 The stream is positioned at the beginning of the file.
86 .TP
87 .B w+
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
91 the file.
92 .TP
93 .B a
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.
97 .TP
98 .B a+
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.
103 .PP
104 The
105 .I mode
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
108 described above.
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
115 environments.)
116 .PP
117 See NOTES below for details of glibc extensions for
118 .IR mode .
119 .PP
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
123 .BR umask (2)).
124 .PP
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
132 .BR fseek (3)
133 or
134 .BR fgetpos (3)
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.
139 .PP
140 Opening a file in append mode (\fBa\fP as the first character of
141 .IR mode )
142 causes all subsequent write operations to this stream to occur
143 at end-of-file, as if preceded the call:
144 .nf
145
146     fseek(stream,0,SEEK_END);
147 .fi
148 .PP
149 The
150 .BR fdopen ()
151 function associates a stream with the existing file descriptor,
152 .IR fd .
153 The
154 .I mode
155 of the stream (one of the values "r", "r+", "w", "w+", "a", "a+")
156 must be compatible with the mode of the file descriptor.
157 The file position indicator of the new stream is set to that
158 belonging to
159 .IR fd ,
160 and the error and end-of-file indicators are cleared.
161 Modes "w" or "w+" do not cause truncation of the file.
162 The file descriptor is not dup'ed, and will be closed when
163 the stream created by
164 .BR fdopen ()
165 is closed.
166 The result of applying
167 .BR fdopen ()
168 to a shared memory object is undefined.
169 .PP
170 The
171 .BR freopen ()
172 function opens the file whose name is the string pointed to by
173 .I path
174 and associates the stream pointed to by
175 .I stream
176 with it.
177 The original stream (if it exists) is closed.
178 The
179 .I mode
180 argument is used just as in the
181 .BR fopen ()
182 function.
183 The primary use of the
184 .BR freopen ()
185 function is to change the file associated with a standard text stream
186 .RI ( stderr ", " stdin ", or " stdout ).
187 .SH "RETURN VALUE"
188 Upon successful completion
189 .BR fopen (),
190 .BR fdopen ()
191 and
192 .BR freopen ()
193 return a
194 .I FILE
195 pointer.
196 Otherwise, NULL is returned and
197 .I errno
198 is set to indicate the error.
199 .SH ERRORS
200 .TP
201 .B EINVAL
202 The
203 .I mode
204 provided to
205 .BR fopen (),
206 .BR fdopen (),
207 or
208 .BR freopen ()
209 was invalid.
210 .PP
211 The
212 .BR fopen (),
213 .BR fdopen ()
214 and
215 .BR freopen ()
216 functions may also fail and set
217 .I errno
218 for any of the errors specified for the routine
219 .BR malloc (3).
220 .PP
221 The
222 .BR fopen ()
223 function may also fail and set
224 .I errno
225 for any of the errors specified for the routine
226 .BR open (2).
227 .PP
228 The
229 .BR fdopen ()
230 function may also fail and set
231 .I errno
232 for any of the errors specified for the routine
233 .BR fcntl (2).
234 .PP
235 The
236 .BR freopen ()
237 function may also fail and set
238 .I errno
239 for any of the errors specified for the routines
240 .BR open (2),
241 .BR fclose (3)
242 and
243 .BR fflush (3).
244 .SH "CONFORMING TO"
245 The
246 .BR fopen ()
247 and
248 .BR freopen ()
249 functions conform to C89.
250 The
251 .BR fdopen ()
252 function conforms to POSIX.1-1990.
253 .SH NOTES
254 .SS Glibc Notes
255 The GNU C library allows the following extensions for the string specified in
256 .IR mode :
257 .TP
258 .BR c " (since glibc 2.3.3)"
259 Do not make the open operation,
260 or subsequent read and write operations,
261 thread cancellation points.
262 This flag is ignored for
263 .BR fdopen ().
264 .TP
265 .BR e " (since glibc 2.7)"
266 Open the file with the
267 .B O_CLOEXEC
268 flag.
269 See
270 .BR open (2)
271 for more information.
272 This flag is ignored for
273 .BR fdopen ().
274 .TP
275 .BR m " (since glibc 2.3)"
276 Attempt to access the file using
277 .BR mmap (2),
278 rather than I/O system calls
279 .RB ( read (2),
280 .BR write (2)).
281 Currently,
282 .\" As at glibc 2.4:
283 use of
284 .BR mmap (2)
285 is only attempted for a file opened for reading.
286 .TP
287 .B x
288 .\" Since glibc 2.0?
289 .\" FIXME C11 specifies this flag
290 Open the file exclusively
291 (like the
292 .B O_EXCL
293 flag of
294 .BR open (2)).
295 If the file already exists,
296 .BR fopen ()
297 fails, and sets
298 .I errno
299 to
300 .BR EEXIST .
301 This flag is ignored for
302 .BR fdopen ().
303 .PP
304 In addition to the above characters,
305 .BR fopen ()
306 and
307 .BR freopen ()
308 support the following syntax
309 in
310 .IR mode :
311
312 .BI "    ,ccs=" string
313
314 The given
315 .I string
316 is taken as the name of a coded character set and
317 the stream is marked as wide-oriented.
318 Thereafter, internal conversion functions convert I/O
319 to and from the character set
320 .IR string .
321 If the
322 .BI ,ccs= string
323 syntax is not specified,
324 then the wide-orientation of the stream is
325 determined by the first file operation.
326 If that operation is a wide-character operation,
327 the stream is marked wide-oriented,
328 and functions to convert to the coded character set are loaded.
329 .SH BUGS
330 In versions of glibc before
331 .SH BUGS
332 When parsing for individual flag characters in
333 .IR mode
334 (i.e., the characters preceding the "ccs" specification"),
335 the glibc implementation of
336 .\" FIXME http://sourceware.org/bugzilla/show_bug.cgi?id=12685
337 .BR fopen ()
338 and
339 .BR freopen ()
340 limits the number of characters examined in
341 .I mode
342 to 7 (or, in glibc versions before 2.14, to 6,
343 which was not enough to include possible specifications such as "rb+cmxe").
344 The current implementation of
345 .BR fdopen ()
346 parses at most 5 characters in in
347 .IR mode .
348 .SH "SEE ALSO"
349 .BR open (2),
350 .BR fclose (3),
351 .BR fileno (3),
352 .BR fmemopen (3),
353 .BR fopencookie (3)