1 .\" $NetBSD: fopen.3,v 1.24 2010/04/05 21:34:47 joerg Exp $
3 .\" Copyright (c) 1990, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" This code is derived from software contributed to Berkeley by
7 .\" Chris Torek and the American National Standards Committee X3,
8 .\" on Information Processing Systems.
10 .\" Redistribution and use in source and binary forms, with or without
11 .\" modification, are permitted provided that the following conditions
13 .\" 1. Redistributions of source code must retain the above copyright
14 .\" notice, this list of conditions and the following disclaimer.
15 .\" 2. Redistributions in binary form must reproduce the above copyright
16 .\" notice, this list of conditions and the following disclaimer in the
17 .\" documentation and/or other materials provided with the distribution.
18 .\" 3. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" @(#)fopen.3 8.1 (Berkeley) 6/4/93
43 .Nd stream open functions
49 .Fn fopen "const char * restrict path" "const char * restrict mode"
51 .Fn fdopen "int fildes" "const char *mode"
53 .Fn freopen "const char * restrict path" "const char * restrict mode" "FILE * restrict stream"
58 opens the file whose name is the string pointed to by
60 and associates a stream with it.
64 points to a string beginning with one of the following
65 sequences (Additional characters may follow these sequences.):
66 .Bl -tag -width indent
70 Open for reading and writing.
73 Truncate file to zero length or create file.
75 Open for reading and writing.
76 Truncate file to zero length or create file.
78 Append; open for writing.
79 The file is created if it does not exist.
81 Append; open for reading and writing.
82 The file is created if it does not exist.
87 string can also include the letter ``b'' either as a last character or
88 as a character between the characters in any of the two-character strings
90 This is strictly for compatibility with
92 and has no effect; the ``b'' is ignored.
94 The letter ``f'' in the mode string restricts fopen to regular
95 files; if the file opened is not a regular file,
102 Any created files will have mode
115 as modified by the process'
119 Opening a file with append mode causes all subsequent writes to it
120 to be forced to the then current end of file, regardless of intervening
121 repositioning of the stream.
127 functions initially position the stream at the start of the file
128 unless the file is opened with append mode,
129 in which case the stream is initially positioned at the end of the file.
130 .\" PR 6072 claims this paragraph is not correct.
132 .\" Reads and writes may be intermixed on read/write streams in any order,
133 .\" and do not require an intermediate seek as in previous versions of
135 .\" This is not portable to other systems, however;
138 .\" a file positioning function intervene between output and input, unless
139 .\" an input operation encounters end-of-file.
143 function associates a stream with the existing file descriptor,
147 of the stream must be compatible with the mode of the file descriptor.
148 The stream is positioned at the file offset of the file descriptor.
153 opens the file whose name is the string pointed to by
155 and associates the stream pointed to by
158 The original stream (if it exists) is closed.
161 argument is used just as in the
164 The primary use of the
167 is to change the file associated with a
174 Upon successful completion
184 is returned and the global variable
186 is set to indicate the error.
199 The file is not a regular file and the character ``f'' is specified
209 may also fail and set
211 for any of the errors specified for the routine
217 may also fail and set
219 for any of the errors specified for the routine
225 may also fail and set
227 for any of the errors specified for the routine
233 may also fail and set
235 for any of the errors specified for the routines
261 with error checking should
264 in case of failure, and
266 the resulting FILE * in case of success.
271 if ((file = fdopen(fd, "r")) != NULL) {
272 /* perform operations on the FILE * */
275 /* failure, report the error */