OSDN Git Service

LDP: Update original to LDP v3.79
[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 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
11 .\" are met:
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\"    notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\"    notice, this list of conditions and the following disclaimer in the
16 .\"    documentation and/or other materials provided with the distribution.
17 .\" 3. All advertising materials mentioning features or use of this software
18 .\"    must display the following acknowledgement:
19 .\"     This product includes software developed by the University of
20 .\"     California, Berkeley and its contributors.
21 .\" 4. Neither the name of the University nor the names of its contributors
22 .\"    may be used to endorse or promote products derived from this software
23 .\"    without specific prior written permission.
24 .\"
25 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 .\" SUCH DAMAGE.
36 .\" %%%LICENSE_END
37 .\"
38 .\"     @(#)fopen.3     6.8 (Berkeley) 6/29/91
39 .\"
40 .\" Converted for Linux, Mon Nov 29 15:22:01 1993, faith@cs.unc.edu
41 .\" Modified, aeb, 960421, 970806
42 .\" Modified, joey, aeb, 2002-01-03
43 .\"
44 .TH FOPEN 3  2012-04-22 "GNU" "Linux Programmer's Manual"
45 .SH NAME
46 fopen, fdopen, freopen \- stream open functions
47 .SH SYNOPSIS
48 .nf
49 .B #include <stdio.h>
50 .sp
51 .BI "FILE *fopen(const char *" path ", const char *" mode );
52
53 .BI "FILE *fdopen(int " fd ", const char *" mode );
54
55 .BI "FILE *freopen(const char *" path ", const char *" mode ", FILE *" stream );
56 .fi
57 .sp
58 .in -4n
59 Feature Test Macro Requirements for glibc (see
60 .BR feature_test_macros (7)):
61 .in
62 .sp
63 .BR fdopen ():
64 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
65 .SH DESCRIPTION
66 The
67 .BR fopen ()
68 function opens the file whose name is the string pointed to by
69 .I path
70 and associates a stream with it.
71 .PP
72 The argument
73 .I mode
74 points to a string beginning with one of the following sequences
75 (possibly followed by additional characters, as described below):
76 .TP
77 .B r
78 Open text file for reading.
79 The stream is positioned at the beginning of the file.
80 .TP
81 .B r+
82 Open for reading and writing.
83 The stream is positioned at the beginning of the file.
84 .TP
85 .B w
86 Truncate file to zero length or create text file for writing.
87 The stream is positioned at the beginning of the file.
88 .TP
89 .B w+
90 Open for reading and writing.
91 The file is created if it does not exist, otherwise it is truncated.
92 The stream is positioned at the beginning of
93 the file.
94 .TP
95 .B a
96 Open for appending (writing at end of file).
97 The file is created if it does not exist.
98 The stream is positioned at the end of the file.
99 .TP
100 .B a+
101 Open for reading and appending (writing at end of file).
102 The file is created if it does not exist.
103 The initial file position for reading is at the beginning of the file,
104 but output is always appended to the end of the file.
105 .PP
106 The
107 .I mode
108 string can also include the letter \(aqb\(aq either as a last character or as
109 a character between the characters in any of the two-character strings
110 described above.
111 This is strictly for compatibility with C89
112 and has no effect; the \(aqb\(aq is ignored on all POSIX
113 conforming systems, including Linux.
114 (Other systems may treat text files and binary files differently,
115 and adding the \(aqb\(aq may be a good idea if you do I/O to a binary
116 file and expect that your program may be ported to non-UNIX
117 environments.)
118 .PP
119 See NOTES below for details of glibc extensions for
120 .IR mode .
121 .PP
122 Any created files will have mode
123 .BR S_IRUSR " | " S_IWUSR " | "  S_IRGRP " | "  S_IWGRP " | " S_IROTH " | " S_IWOTH
124 (0666), as modified by the process's umask value (see
125 .BR umask (2)).
126 .PP
127 Reads and writes may be intermixed on read/write streams in any order.
128 Note that ANSI C requires that a file positioning function intervene
129 between output and input, unless an input operation encounters end-of-file.
130 (If this condition is not met, then a read is allowed to return the
131 result of writes other than the most recent.)
132 Therefore it is good practice (and indeed sometimes necessary
133 under Linux) to put an
134 .BR fseek (3)
135 or
136 .BR fgetpos (3)
137 operation between write and read operations on such a stream.
138 This operation may be an apparent no-op
139 (as in \fIfseek(..., 0L, SEEK_CUR)\fP
140 called for its synchronizing side effect).
141 .PP
142 Opening a file in append mode (\fBa\fP as the first character of
143 .IR mode )
144 causes all subsequent write operations to this stream to occur
145 at end-of-file, as if preceded the call:
146 .nf
147
148     fseek(stream, 0, SEEK_END);
149 .fi
150 .PP
151 The
152 .BR fdopen ()
153 function associates a stream with the existing file descriptor,
154 .IR fd .
155 The
156 .I mode
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
160 belonging to
161 .IR fd ,
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
166 .BR fdopen ()
167 is closed.
168 The result of applying
169 .BR fdopen ()
170 to a shared memory object is undefined.
171 .PP
172 The
173 .BR freopen ()
174 function opens the file whose name is the string pointed to by
175 .I path
176 and associates the stream pointed to by
177 .I stream
178 with it.
179 The original stream (if it exists) is closed.
180 The
181 .I mode
182 argument is used just as in the
183 .BR fopen ()
184 function.
185 The primary use of the
186 .BR freopen ()
187 function is to change the file associated with a standard text stream
188 .RI ( stderr ", " stdin ", or " stdout ).
189 .SH RETURN VALUE
190 Upon successful completion
191 .BR fopen (),
192 .BR fdopen ()
193 and
194 .BR freopen ()
195 return a
196 .I FILE
197 pointer.
198 Otherwise, NULL is returned and
199 .I errno
200 is set to indicate the error.
201 .SH ERRORS
202 .TP
203 .B EINVAL
204 The
205 .I mode
206 provided to
207 .BR fopen (),
208 .BR fdopen (),
209 or
210 .BR freopen ()
211 was invalid.
212 .PP
213 The
214 .BR fopen (),
215 .BR fdopen ()
216 and
217 .BR freopen ()
218 functions may also fail and set
219 .I errno
220 for any of the errors specified for the routine
221 .BR malloc (3).
222 .PP
223 The
224 .BR fopen ()
225 function may also fail and set
226 .I errno
227 for any of the errors specified for the routine
228 .BR open (2).
229 .PP
230 The
231 .BR fdopen ()
232 function may also fail and set
233 .I errno
234 for any of the errors specified for the routine
235 .BR fcntl (2).
236 .PP
237 The
238 .BR freopen ()
239 function may also fail and set
240 .I errno
241 for any of the errors specified for the routines
242 .BR open (2),
243 .BR fclose (3),
244 and
245 .BR fflush (3).
246 .SH CONFORMING TO
247 The
248 .BR fopen ()
249 and
250 .BR freopen ()
251 functions conform to C89.
252 The
253 .BR fdopen ()
254 function conforms to POSIX.1-1990.
255 .SH NOTES
256 .SS Glibc notes
257 The GNU C library allows the following extensions for the string specified in
258 .IR mode :
259 .TP
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.
264 This flag is ignored for
265 .BR fdopen ().
266 .TP
267 .BR e " (since glibc 2.7)"
268 Open the file with the
269 .B O_CLOEXEC
270 flag.
271 See
272 .BR open (2)
273 for more information.
274 This flag is ignored for
275 .BR fdopen ().
276 .TP
277 .BR m " (since glibc 2.3)"
278 Attempt to access the file using
279 .BR mmap (2),
280 rather than I/O system calls
281 .RB ( read (2),
282 .BR write (2)).
283 Currently,
284 .\" As at glibc 2.4:
285 use of
286 .BR mmap (2)
287 is attempted only for a file opened for reading.
288 .TP
289 .B x
290 .\" Since glibc 2.0?
291 .\" FIXME . C11 specifies this flag
292 Open the file exclusively
293 (like the
294 .B O_EXCL
295 flag of
296 .BR open (2)).
297 If the file already exists,
298 .BR fopen ()
299 fails, and sets
300 .I errno
301 to
302 .BR EEXIST .
303 This flag is ignored for
304 .BR fdopen ().
305 .PP
306 In addition to the above characters,
307 .BR fopen ()
308 and
309 .BR freopen ()
310 support the following syntax
311 in
312 .IR mode :
313
314 .BI "    ,ccs=" string
315
316 The given
317 .I string
318 is taken as the name of a coded character set and
319 the stream is marked as wide-oriented.
320 Thereafter, internal conversion functions convert I/O
321 to and from the character set
322 .IR string .
323 If the
324 .BI ,ccs= string
325 syntax is not specified,
326 then the wide-orientation of the stream is
327 determined by the first file operation.
328 If that operation is a wide-character operation,
329 the stream is marked wide-oriented,
330 and functions to convert to the coded character set are loaded.
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
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)
354 .SH COLOPHON
355 This page is part of release 3.79 of the Linux
356 .I man-pages
357 project.
358 A description of the project,
359 information about reporting bugs,
360 and the latest version of this page,
361 can be found at
362 \%http://www.kernel.org/doc/man\-pages/.