1 .\" $NetBSD: fopen.3,v 1.28 2012/07/02 20:02:43 wiz 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.):
68 Append; open for writing.
69 The file is created if it does not exist.
71 Append; open for reading and writing.
72 The file is created if it does not exist.
76 Open for reading and writing.
79 Truncate file to zero length or create file.
81 Open for reading and writing.
82 Truncate file to zero length or create file.
87 string can also include one of the following letters:
92 may appear either as a last character or as a character between the
93 characters in any of the two-character strings described above.
94 This is strictly for compatibility with
96 and has no effect; the
102 in the mode string sets the close-on-exec flag in the file descriptors of
103 the newly opened file files; if the operation fails,
112 in the mode string restricts
114 to regular files; if the file opened is not a regular file,
122 Any created files will have mode
135 as modified by the process'
139 Opening a file with append mode causes all subsequent writes to it
140 to be forced to the then current end of file, regardless of intervening
141 repositioning of the stream.
147 functions initially position the stream at the start of the file
148 unless the file is opened with append mode,
149 in which case the stream is initially positioned at the end of the file.
150 .\" PR 6072 claims this paragraph is not correct.
152 .\" Reads and writes may be intermixed on read/write streams in any order,
153 .\" and do not require an intermediate seek as in previous versions of
155 .\" This is not portable to other systems, however;
158 .\" a file positioning function intervene between output and input, unless
159 .\" an input operation encounters end-of-file.
163 function associates a stream with the existing file descriptor,
167 of the stream must be compatible with the mode of the file descriptor.
168 The stream is positioned at the file offset of the file descriptor.
173 opens the file whose name is the string pointed to by
175 and associates the stream pointed to by
178 The original stream (if it exists) is closed.
181 argument is used just as in the
184 The primary use of the
187 is to change the file associated with a
194 Upon successful completion
204 is returned and the global variable
206 is set to indicate the error.
208 The functions may fail if:
211 The file is not a regular file and the character ``f'' is specified
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 routine
241 may also fail and set
243 for any of the errors specified for the routine
249 may also fail and set
251 for any of the errors specified for the routines
269 All three functions are specified in
274 with error checking should
277 in case of failure, and
279 the resulting FILE * in case of success.
284 if ((file = fdopen(fd, "r")) != NULL) {
285 /* perform operations on the FILE * */
288 /* failure, report the error */