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
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"
55 .Fn fmemopen "void *restrict *buf" "size_t size" "const char * restrict mode"
60 opens the file whose name is the string pointed to by
62 and associates a stream with it.
66 points to a string beginning with one of the following letters:
67 .Bl -tag -width indent
70 The stream is positioned at the beginning of the file.
71 Fail if the file does not exist.
74 The stream is positioned at the beginning of the file.
75 Create the file if it does not exist.
78 The stream is positioned at the end of the file.
79 Subsequent writes to the file will always end up at the then current
80 end of file, irrespective of any intervening
83 Create the file if it does not exist.
93 opens the file for both reading and writing.
102 call to fail if the file already exists.
110 flag on the underlying file descriptor.
114 string can also include the letter
119 This is strictly for compatibility with
121 and has effect only for
123 ; otherwise the ``b'' is ignored.
125 Any created files will have mode
138 as modified by the process'
142 Reads and writes may be intermixed on read/write streams in any order,
143 and do not require an intermediate seek as in previous versions of
145 This is not portable to other systems, however;
148 a file positioning function intervene between output and input, unless
149 an input operation encounters end-of-file.
153 function associates a stream with the existing file descriptor,
156 of the stream must be compatible with the mode of the file descriptor.
159 mode option is ignored.
162 mode option is present, the
164 flag is set, otherwise it remains unchanged.
165 When the stream is closed via
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
190 attempts to re-open the file associated with
193 The new mode must be compatible with the mode that the stream was originally
195 Streams open for reading can only be re-opened for reading,
196 streams open for writing can only be re-opened for writing,
197 and streams open for reading and writing can be re-opened in any mode.
200 mode option is not meaningful in this context.
202 The primary use of the
205 is to change the file associated with a
207 .Dv ( stderr , stdin ,
214 associates the buffer given by the
218 arguments with a stream.
221 argument is either a null pointer or point to a buffer that
225 If a null pointer is specified as the
231 bytes of memory. This buffer is automatically freed when the
232 stream is closed. Buffers can be opened in text-mode (default) or binary-mode
233 (if ``b'' is present in the second or third position of the
235 argument). Buffers opened in text-mode make sure that writes are terminated with
236 a NULL byte, if the last write hasn't filled up the whole buffer. Buffers
237 opened in binary-mode never append a NULL byte.
239 Upon successful completion
249 is returned and the global variable
251 is set to indicate the error.
274 may also fail and set
276 for any of the errors specified for the routine
282 may also fail and set
284 for any of the errors specified for the routine
290 may also fail and set
292 for any of the errors specified for the routine
298 may also fail and set
300 for any of the errors specified for the routines
309 may also fail and set
328 with the exception of the
330 mode option which conforms to
339 mode option does not conform to any standard
340 but is also supported by glibc.
346 The ``b'' mode does not conform to any standard
347 but is also supported by glibc.