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. 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.20 2007/01/09 00:28:06 imp Exp $
34 .\" $DragonFly: src/lib/libc/stdio/fopen.3,v 1.4 2007/04/04 18:36:55 swildner Exp $
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 *path" "const char *mode" "FILE *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
68 Open file for reading.
69 The stream is positioned at the beginning of the file.
71 Open file for writing.
72 Creates the file if it does not already exist,
73 truncates the file if it does.
76 Creates the file if it does not already exist.
77 Seeks to end of the file.
78 Subsequent writes to the file will always end up at the then current
79 end of file, irrespective of any intervening
84 Additional flags may modify the primary mode as follows:
85 .Bl -tag -width indent
87 The file will be opened for both reading and writing.
88 For example 'r+' or 'w+'.
90 The open will fail if the file already exists (C11).
93 Close-on-exec. The underlying descriptor will automatically be closed on
94 any exec() (glibc compatibility).
96 Binary mode. On UNIX systems file reading and writing is always in binary
97 mode so this flag has no effect.
102 string can also include the letter ``b'' either as a third character or
103 as a character between the characters in any of the two-character strings
105 This is strictly for compatibility with
107 and has no effect; the ``b'' is ignored.
109 Any created files will have mode
122 as modified by the process'
126 Reads and writes may be intermixed on read/write streams in any order,
127 and do not require an intermediate seek as in previous versions of
129 This is not portable to other systems, however;
132 a file positioning function intervene between output and input, unless
133 an input operation encounters end-of-file.
137 function associates a stream with the existing file descriptor,
140 of the stream must be compatible with the mode of the file descriptor.
141 When the stream is closed via
149 opens the file whose name is the string pointed to by
151 and associates the stream pointed to by
154 The original stream (if it exists) is closed.
157 argument is used just as in the
166 attempts to re-open the file associated with
169 The new mode must be compatible with the mode that the stream was originally
171 .Bl -bullet -offset indent
173 Streams originally opened with mode
175 can only be reopened with that same mode.
177 Streams originally opened with mode
179 can be reopened with the same mode, or mode
182 Streams originally opened with mode
184 can be reopened with the same mode, or mode
187 Streams originally opened with mode
192 can be reopened with any mode.
195 The primary use of the
198 is to change the file associated with a
200 .Dv ( stderr , stdin ,
204 Upon successful completion
214 is returned and the global variable
216 is set to indicate the error.
237 may also fail and set
239 for any of the errors specified for the routine
245 may also fail and set
247 for any of the errors specified for the routine
253 may also fail and set
255 for any of the errors specified for the routine
261 may also fail and set
263 for any of the errors specified for the routines