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 .\" 3. All advertising materials mentioning features or use of this software
17 .\" must display the following acknowledgement:
18 .\" This product includes software developed by the University of
19 .\" California, Berkeley and its contributors.
20 .\" 4. Neither the name of the University nor the names of its contributors
21 .\" may be used to endorse or promote products derived from this software
22 .\" without specific prior written permission.
24 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .\" @(#)fopen.3 8.1 (Berkeley) 6/4/93
37 .\" $FreeBSD: src/lib/libc/stdio/fopen.3,v 1.18 2003/01/26 10:01:59 tjr Exp $
46 .Nd stream open functions
54 .Fa "const char *mode"
58 .Fa "const char *restrict filename"
59 .Fa "const char *restrict mode"
63 .Fa "const char *restrict filename"
64 .Fa "const char *restrict mode"
65 .Fa "FILE *restrict stream"
71 opens the file whose name is the string pointed to by
73 and associates a stream with it.
77 points to a string beginning with one of the following
78 sequences (Additional characters may follow these sequences.):
79 .Bl -tag -width indent
81 Open text file for reading.
82 The stream is positioned at the beginning of the file.
84 Open for reading and writing.
85 The stream is positioned at the beginning of the file.
87 Truncate file to zero length or create text file for writing.
88 The stream is positioned at the beginning of the file.
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 the file.
95 The file is created if it does not exist.
96 The stream is positioned at the end of the file.
97 Subsequent writes to the file will always end up at the then current
98 end of file, irrespective of any intervening
102 Open for reading and writing.
103 The file is created if it does not exist.
104 The stream is positioned at the end of the file.
105 Subsequent writes to the file will always end up at the then current
106 end of file, irrespective of any intervening
113 string can also include the letter ``b'' either as a third character or
114 as a character between the characters in any of the two-character strings
116 This is strictly for compatibility with
118 and has no effect; the ``b'' is ignored.
120 Finally, as an extension to the standards (and thus may not be portable),
122 string may end with the letter ``x'', which insists on creating a new file
123 when used with ``w'' or ``a''.
126 exists, then an error is returned (this is the equivalent of specifying
131 Any created files will have mode
144 as modified by the process'
148 Reads and writes may be intermixed on read/write streams in any order,
149 and do not require an intermediate seek as in previous versions of
151 This is not portable to other systems, however;
154 a file positioning function intervene between output and input, unless
155 an input operation encounters end-of-file.
159 function associates a stream with the existing file descriptor,
162 of the stream must be compatible with the mode of the file descriptor.
163 When the stream is closed via
171 opens the file whose name is the string pointed to by
173 and associates the stream pointed to by
176 The original stream (if it exists) is closed.
179 argument is used just as in the
188 attempts to re-open the file associated with
191 The new mode must be compatible with the mode that the stream was originally
193 .Bl -bullet -offset indent
195 Streams originally opened with mode
197 can only be reopened with that same mode.
199 Streams originally opened with mode
201 can be reopened with the same mode, or mode
204 Streams originally opened with mode
206 can be reopened with the same mode, or mode
209 Streams originally opened with mode
214 can be reopened with any mode.
217 The primary use of the
220 is to change the file associated with a
222 .Dv ( stderr , stdin ,
226 Upon successful completion
236 is returned and the global variable
238 is set to indicate the error.
259 may also fail and set
261 for any of the errors specified for the routine
267 may also fail and set
269 for any of the errors specified for the routine
275 may also fail and set
277 for any of the errors specified for the routine
283 may also fail and set
285 for any of the errors specified for the routines