]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/fopen.3
ffix
[thirdparty/man-pages.git] / man3 / fopen.3
CommitLineData
fea681da
MK
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
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.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
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.
23.\"
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
34.\" SUCH DAMAGE.
35.\"
36.\" @(#)fopen.3 6.8 (Berkeley) 6/29/91
37.\"
38.\" Converted for Linux, Mon Nov 29 15:22:01 1993, faith@cs.unc.edu
39.\" Modified, aeb, 960421, 970806
40.\" Modified, joey, aeb, 2002-01-03
41.\"
69962544 42.TH FOPEN 3 2006-05-04 "GNU" "Linux Programmer's Manual"
fea681da
MK
43.SH NAME
44fopen, fdopen, freopen \- stream open functions
45.SH SYNOPSIS
46.B #include <stdio.h>
47.sp
48.BI "FILE *fopen(const char *" path ", const char *" mode );
49.br
50.BI "FILE *fdopen(int " fildes ", const char *" mode );
51.br
52.BI "FILE *freopen(const char *" path ", const char *" mode ", FILE *" stream );
53.SH DESCRIPTION
54The
e511ffb6 55.BR fopen ()
fea681da
MK
56function opens the file whose name is the string pointed to by
57.I path
58and associates a stream with it.
59.PP
60The argument
61.I mode
62points to a string beginning with one of the following sequences
63(Additional characters may follow these sequences.):
64.TP
65.B r
c13182ef
MK
66Open text file for reading.
67The stream is positioned at the beginning of the file.
fea681da
MK
68.TP
69.B r+
c13182ef
MK
70Open for reading and writing.
71The stream is positioned at the beginning of the file.
fea681da
MK
72.TP
73.B w
c13182ef
MK
74Truncate file to zero length or create text file for writing.
75The stream is positioned at the beginning of the file.
fea681da
MK
76.TP
77.B w+
c13182ef
MK
78Open for reading and writing.
79The file is created if it does not exist, otherwise it is truncated.
80The stream is positioned at the beginning of
fea681da
MK
81the file.
82.TP
83.B a
c13182ef
MK
84Open for appending (writing at end of file).
85The file is created if it does not exist.
86The stream is positioned at the end of the file.
fea681da
MK
87.TP
88.B a+
c13182ef
MK
89Open for reading and appending (writing at end of file).
90The file is created if it does not exist.
be9dccdf
MK
91The initial file position for reading is at the beginning of the file,
92but output is always appended to the end of the file.
fea681da
MK
93.PP
94The
95.I mode
928410b4 96string can also include the letter 'b' either as a last character or as
fea681da 97a character between the characters in any of the two-character strings
c13182ef
MK
98described above.
99This is strictly for compatibility with C89
928410b4 100and has no effect; the 'b' is ignored on all POSIX
fea681da
MK
101conforming systems, including Linux.
102(Other systems may treat text files and binary files differently,
928410b4 103and adding the 'b' may be a good idea if you do I/O to a binary
fea681da
MK
104file and expect that your program may be ported to non-Unix
105environments.)
106.PP
107Any created files will have mode
108.BR S_IRUSR \&| S_IWUSR \&| S_IRGRP \&| S_IWGRP \&| S_IROTH \&| S_IWOTH
fc7ba057 109(0666), as modified by the process's umask value (see
fea681da
MK
110.BR umask (2)).
111.PP
112Reads and writes may be intermixed on read/write streams in any order.
113Note that ANSI C requires that a file positioning function intervene
114between output and input, unless an input operation encounters end-of-file.
115(If this condition is not met, then a read is allowed to return the
116result of writes other than the most recent.)
117Therefore it is good practice (and indeed sometimes necessary
118under Linux) to put an
fb186734 119.BR fseek (3)
fea681da 120or
fb186734 121.BR fgetpos (3)
c13182ef 122operation between write and read operations on such a stream.
2f0af33b
MK
123This operation may be an apparent no-op
124(as in \fIfseek(..., 0L, SEEK_CUR)\fR
125called for its synchronizing side effect.
fea681da
MK
126.PP
127Opening a file in append mode (\fBa\fR as the first character of
128.IR mode )
129causes all subsequent write operations to this stream to occur
130at end-of-file, as if preceded by an
131.RS
132fseek(stream,0,SEEK_END);
133.RE
134call.
135.PP
136The
e511ffb6 137.BR fdopen ()
fea681da
MK
138function associates a stream with the existing file descriptor,
139.IR fildes .
140The
141.I mode
142of the stream (one of the values "r", "r+", "w", "w+", "a", "a+")
143must be compatible with the mode of the file descriptor.
144The file position indicator of the new stream is set to that
145belonging to
146.IR fildes ,
147and the error and end-of-file indicators are cleared.
148Modes "w" or "w+" do not cause truncation of the file.
149The file descriptor is not dup'ed, and will be closed when
150the stream created by
e511ffb6 151.BR fdopen ()
fea681da
MK
152is closed.
153The result of applying
e511ffb6 154.BR fdopen ()
fea681da
MK
155to a shared memory object is undefined.
156.PP
157The
e511ffb6 158.BR freopen ()
fea681da
MK
159function opens the file whose name is the string pointed to by
160.I path
161and associates the stream pointed to by
162.I stream
c13182ef
MK
163with it.
164The original stream (if it exists) is closed.
165The
fea681da
MK
166.I mode
167argument is used just as in the
e511ffb6 168.BR fopen ()
c13182ef
MK
169function.
170The primary use of the
e511ffb6 171.BR freopen ()
fea681da
MK
172function is to change the file associated with a standard text stream
173.IR "" ( stderr ", " stdin ", or " stdout ).
174.SH "RETURN VALUE"
175Upon successful completion
e511ffb6
MK
176.BR fopen (),
177.BR fdopen ()
fea681da 178and
e511ffb6 179.BR freopen ()
fea681da 180return a
836f07c1 181.I FILE
c13182ef 182pointer.
8478ee02 183Otherwise, NULL is returned and the global variable
fea681da
MK
184.I errno
185is set to indicate the error.
186.SH ERRORS
187.TP
188.B EINVAL
189The
190.I mode
191provided to
e511ffb6
MK
192.BR fopen (),
193.BR fdopen (),
fea681da 194or
e511ffb6 195.BR freopen ()
fea681da
MK
196was invalid.
197.PP
198The
e511ffb6
MK
199.BR fopen (),
200.BR fdopen ()
fea681da 201and
e511ffb6 202.BR freopen ()
fea681da
MK
203functions may also fail and set
204.I errno
205for any of the errors specified for the routine
206.BR malloc (3).
207.PP
208The
e511ffb6 209.BR fopen ()
fea681da
MK
210function may also fail and set
211.I errno
212for any of the errors specified for the routine
213.BR open (2).
214.PP
215The
e511ffb6 216.BR fdopen ()
fea681da
MK
217function may also fail and set
218.I errno
219for any of the errors specified for the routine
220.BR fcntl (2).
221.PP
222The
e511ffb6 223.BR freopen ()
fea681da
MK
224function may also fail and set
225.I errno
226for any of the errors specified for the routines
227.BR open (2),
228.BR fclose (3)
229and
230.BR fflush (3).
231.SH "CONFORMING TO"
232The
e511ffb6 233.BR fopen ()
fea681da 234and
e511ffb6 235.BR freopen ()
c13182ef 236functions conform to C89.
1eb85d14 237The
e511ffb6 238.BR fdopen ()
68e1685c 239function conforms to POSIX.1-1990.
d597239c
MK
240.SH NOTES
241.SS Glibc Notes
3ed4fcbd 242The GNU C library allows the following extensions for the string specified in
a30e090e
MK
243.IR mode :
244.TP
bcd70adc 245.BR c " (since glibc 2.3.3)"
c13182ef 246Do not make the open operation,
bcd70adc
MK
247or subsequent read and write operations,
248thread cancellation points.
249.TP
250.BR m " (since glibc 2.3)"
c13182ef 251Attempt to access the file using
bcd70adc 252.BR mmap (2),
c13182ef 253rather than I/O system calls
bcd70adc
MK
254.RB ( read (2),
255.BR write (2)).
c13182ef 256Currently,
bcd70adc 257.\" As at glibc 2.4:
2d2c82e1 258use of
bcd70adc 259.BR mmap (2)
2d2c82e1 260is only attempted for a file opened for reading.
bcd70adc 261.TP
a30e090e 262.B x
accdf645 263.\" Since glibc 2.0?
a30e090e 264Open the file exclusively
c13182ef 265(like the
a30e090e
MK
266.B O_EXCL
267flag of
268.BR open (2)).
269If the file already exists,
270.BR fopen ()
271fails, and sets
272.I errno
273to
274.BR EEXIST .
bcd70adc
MK
275This flag is ignored for
276.BR fdopen ().
218e46f8 277.\" FIXME document /,ccs= charset/
fea681da
MK
278.SH "SEE ALSO"
279.BR open (2),
280.BR fclose (3),
281.BR fileno (3)