1 .\" Copyright (c) 1990, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
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.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
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 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)fopen.3 8.1 (Berkeley) 6/4/93
33 .\" $FreeBSD: src/lib/libc/stdio/fopen.3,v 1.21 2009/09/09 19:38:19 ed Exp $
42 .Nd stream open functions
50 .Fa "const char *mode"
54 .Fa "const char *restrict filename"
55 .Fa "const char *restrict mode"
59 .Fa "const char *restrict filename"
60 .Fa "const char *restrict mode"
61 .Fa "FILE *restrict stream"
67 opens the file whose name is the string pointed to by
69 and associates a stream with it.
73 points to a string beginning with one of the following
74 sequences (Additional characters may follow these sequences.):
75 .Bl -tag -width indent
77 Open text file for reading.
78 The stream is positioned at the beginning of the file.
80 Open for reading and writing.
81 The stream is positioned at the beginning of the file.
83 Truncate to zero length or create text file for writing.
84 The stream is positioned at the beginning of the file.
86 Open for reading and writing.
87 The file is created if it does not exist, otherwise it is truncated.
88 The stream is positioned at the beginning of the file.
91 The file is created if it does not exist.
92 The stream is positioned at the end of the file.
93 Subsequent writes to the file will always end up at the then current
94 end of file, irrespective of any intervening
98 Open for reading and writing.
99 The file is created if it does not exist.
100 The stream is positioned at the end of the file.
101 Subsequent writes to the file will always end up at the then current
102 end of file, irrespective of any intervening
109 string can also include the letter ``b'' either as last character or
110 as a character between the characters in any of the two-character strings
112 This is strictly for compatibility with
114 and has no effect; the ``b'' is ignored.
116 Finally, as an extension to the standards (and thus may not be portable),
118 string may end with the letter ``x'', which insists on creating a new file
119 when used with ``w'' or ``a''.
122 exists, then an error is returned (this is the equivalent of specifying
127 Any created files will have mode
140 as modified by the process'
144 Reads and writes may be intermixed on read/write streams in any order,
145 and do not require an intermediate seek as in previous versions of
147 This is not portable to other systems, however;
150 a file positioning function intervene between output and input, unless
151 an input operation encounters end-of-file.
155 function associates a stream with the existing file descriptor,
158 of the stream must be compatible with the mode of the file descriptor.
159 When the stream is closed via
167 opens the file whose name is the string pointed to by
169 and associates the stream pointed to by
172 The original stream (if it exists) is closed.
175 argument is used just as in the
184 attempts to re-open the file associated with
187 The new mode must be compatible with the mode that the stream was originally
189 .Bl -bullet -offset indent
191 Streams originally opened with mode
193 can only be reopened with that same mode.
195 Streams originally opened with mode
197 can be reopened with the same mode, or mode
200 Streams originally opened with mode
202 can be reopened with the same mode, or mode
205 Streams originally opened with mode
210 can be reopened with any mode.
213 The primary use of the
216 is to change the file associated with a
218 .Dv ( stderr , stdin ,
222 Upon successful completion
232 is returned and the global variable
234 is set to indicate the error.
255 may also fail and set
257 for any of the errors specified for the routine
263 may also fail and set
265 for any of the errors specified for the routine
271 may also fail and set
273 for any of the errors specified for the routine
279 may also fail and set
281 for any of the errors specified for the routines