1 .\" $NetBSD: fopen.3,v 1.31 2015/07/15 19:08:43 christos 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,
123 in the mode turns on exclusive open mode to the file
125 which means that the file will not be created if it already exists.
128 Any created files will have mode
141 as modified by the process'
145 Opening a file with append mode causes all subsequent writes to it
146 to be forced to the then current end of file, regardless of intervening
147 repositioning of the stream.
153 functions initially position the stream at the start of the file
154 unless the file is opened with append mode,
155 in which case the stream is initially positioned at the end of the file.
156 .\" PR 6072 claims this paragraph is not correct.
158 .\" Reads and writes may be intermixed on read/write streams in any order,
159 .\" and do not require an intermediate seek as in previous versions of
161 .\" This is not portable to other systems, however;
164 .\" a file positioning function intervene between output and input, unless
165 .\" an input operation encounters end-of-file.
169 function associates a stream with the existing file descriptor,
173 of the stream must be compatible with the mode of the file descriptor.
174 The stream is positioned at the file offset of the file descriptor.
179 opens the file whose name is the string pointed to by
181 and associates the stream pointed to by
184 The original stream (if it exists) is closed.
187 argument is used just as in the
190 The primary use of the
193 is to change the file associated with a
200 Input and output against the opened stream will be fully buffered, unless
201 it refers to an interactive terminal device, or a different kind of buffering
202 is specified in the environment.
205 for additional details.
207 Upon successful completion
217 is returned and the global variable
219 is set to indicate the error.
221 The functions may fail if:
224 The file is not a regular file and the character ``f'' is specified
238 may also fail and set
240 for any of the errors specified for the routine
246 may also fail and set
248 for any of the errors specified for the routine
254 may also fail and set
256 for any of the errors specified for the routine
262 may also fail and set
264 for any of the errors specified for the routines
282 All three functions are specified in
287 with error checking should
290 in case of failure, and
292 the resulting FILE * in case of success.
297 if ((file = fdopen(fd, "r")) != NULL) {
298 /* perform operations on the FILE * */
301 /* failure, report the error */