]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/fopen.3
Various pages: [BSD-4-Clause-UC] Use SPDX-License-Identifier
[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.\"
47009d5e 8.\" SPDX-License-Identifier: BSD-4-Clause-UC
fea681da
MK
9.\"
10.\" @(#)fopen.3 6.8 (Berkeley) 6/29/91
11.\"
12.\" Converted for Linux, Mon Nov 29 15:22:01 1993, faith@cs.unc.edu
13.\" Modified, aeb, 960421, 970806
14.\" Modified, joey, aeb, 2002-01-03
15.\"
1d767b55 16.TH FOPEN 3 2021-03-22 "GNU" "Linux Programmer's Manual"
fea681da
MK
17.SH NAME
18fopen, fdopen, freopen \- stream open functions
19.SH SYNOPSIS
5895e7eb 20.nf
fea681da 21.B #include <stdio.h>
68e4db0a 22.PP
1ac0a9cc
AC
23.BI "FILE *fopen(const char *restrict " pathname \
24", const char *restrict " mode );
47752f33 25.BI "FILE *fdopen(int " fd ", const char *" mode );
1ac0a9cc
AC
26.BI "FILE *freopen(const char *restrict " pathname \
27", const char *restrict " mode ,
28.BI " FILE *restrict " stream );
5895e7eb 29.fi
68e4db0a 30.PP
d39ad78f 31.RS -4
cc4615cc
MK
32Feature Test Macro Requirements for glibc (see
33.BR feature_test_macros (7)):
d39ad78f 34.RE
68e4db0a 35.PP
cc4615cc 36.BR fdopen ():
1dd0d7b4
MK
37.nf
38 _POSIX_C_SOURCE
39.fi
fea681da
MK
40.SH DESCRIPTION
41The
e511ffb6 42.BR fopen ()
fea681da 43function opens the file whose name is the string pointed to by
af500011 44.I pathname
fea681da
MK
45and associates a stream with it.
46.PP
47The argument
48.I mode
49points to a string beginning with one of the following sequences
20e41f3f 50(possibly followed by additional characters, as described below):
fea681da
MK
51.TP
52.B r
c13182ef
MK
53Open text file for reading.
54The stream is positioned at the beginning of the file.
fea681da
MK
55.TP
56.B r+
c13182ef
MK
57Open for reading and writing.
58The stream is positioned at the beginning of the file.
fea681da
MK
59.TP
60.B w
c13182ef
MK
61Truncate file to zero length or create text file for writing.
62The stream is positioned at the beginning of the file.
fea681da
MK
63.TP
64.B w+
c13182ef
MK
65Open for reading and writing.
66The file is created if it does not exist, otherwise it is truncated.
67The stream is positioned at the beginning of
fea681da
MK
68the file.
69.TP
70.B a
c13182ef
MK
71Open for appending (writing at end of file).
72The file is created if it does not exist.
73The stream is positioned at the end of the file.
fea681da
MK
74.TP
75.B a+
c13182ef
MK
76Open for reading and appending (writing at end of file).
77The file is created if it does not exist.
d66e1f2b 78Output is always appended to the end of the file.
492a6a0d 79POSIX is silent on what the initial read position is when using this mode.
d66e1f2b 80For glibc, the initial file position for reading is at
81the beginning of the file, but for Android/BSD/MacOS, the
492a6a0d 82initial file position for reading is at the end of the file.
fea681da
MK
83.PP
84The
85.I mode
f81fb444 86string can also include the letter \(aqb\(aq either as a last character or as
fea681da 87a character between the characters in any of the two-character strings
c13182ef
MK
88described above.
89This is strictly for compatibility with C89
f81fb444 90and has no effect; the \(aqb\(aq is ignored on all POSIX
fea681da
MK
91conforming systems, including Linux.
92(Other systems may treat text files and binary files differently,
f81fb444 93and adding the \(aqb\(aq may be a good idea if you do I/O to a binary
008f1ecc 94file and expect that your program may be ported to non-UNIX
fea681da
MK
95environments.)
96.PP
97004b99
MK
97See NOTES below for details of glibc extensions for
98.IR mode .
99.PP
c67f4a0a 100Any created file will have the mode
cab87712 101.BR S_IRUSR " | " S_IWUSR " | " S_IRGRP " | " S_IWGRP " | " S_IROTH " | " S_IWOTH
fc7ba057 102(0666), as modified by the process's umask value (see
fea681da
MK
103.BR umask (2)).
104.PP
105Reads and writes may be intermixed on read/write streams in any order.
106Note that ANSI C requires that a file positioning function intervene
107between output and input, unless an input operation encounters end-of-file.
108(If this condition is not met, then a read is allowed to return the
109result of writes other than the most recent.)
110Therefore it is good practice (and indeed sometimes necessary
111under Linux) to put an
fb186734 112.BR fseek (3)
fea681da 113or
fb186734 114.BR fgetpos (3)
c13182ef 115operation between write and read operations on such a stream.
2f0af33b 116This operation may be an apparent no-op
a8d55537 117(as in \fIfseek(..., 0L, SEEK_CUR)\fP
c4920f2b 118called for its synchronizing side effect).
fea681da 119.PP
a8d55537 120Opening a file in append mode (\fBa\fP as the first character of
fea681da
MK
121.IR mode )
122causes all subsequent write operations to this stream to occur
96285a93 123at end-of-file, as if preceded the call:
207050fa
MK
124.PP
125.in +4n
126.EX
127fseek(stream, 0, SEEK_END);
128.EE
129.in
a8250b91
MK
130.PP
131The file descriptor associated with the stream is opened as if by a call to
132.BR open (2)
133with the following flags:
134.RS
135.TS
136allbox;
137lb lb
138c l.
139fopen() mode open() flags
140\fIr\fP O_RDONLY
141\fIw\fP O_WRONLY | O_CREAT | O_TRUNC
142\fIa\fP O_WRONLY | O_CREAT | O_APPEND
143\fIr+\fP O_RDWR
144\fIw+\fP O_RDWR | O_CREAT | O_TRUNC
145\fIa+\fP O_RDWR | O_CREAT | O_APPEND
146.TE
147.RE
f6386ed6
MK
148.\"
149.SS fdopen()
fea681da 150The
e511ffb6 151.BR fdopen ()
fea681da 152function associates a stream with the existing file descriptor,
47752f33 153.IR fd .
fea681da
MK
154The
155.I mode
156of the stream (one of the values "r", "r+", "w", "w+", "a", "a+")
157must be compatible with the mode of the file descriptor.
158The file position indicator of the new stream is set to that
159belonging to
47752f33 160.IR fd ,
fea681da
MK
161and the error and end-of-file indicators are cleared.
162Modes "w" or "w+" do not cause truncation of the file.
163The file descriptor is not dup'ed, and will be closed when
164the stream created by
e511ffb6 165.BR fdopen ()
fea681da
MK
166is closed.
167The result of applying
e511ffb6 168.BR fdopen ()
fea681da 169to a shared memory object is undefined.
f6386ed6
MK
170.\"
171.SS freopen()
fea681da 172The
e511ffb6 173.BR freopen ()
fea681da 174function opens the file whose name is the string pointed to by
af500011 175.I pathname
fea681da
MK
176and associates the stream pointed to by
177.I stream
c13182ef
MK
178with it.
179The original stream (if it exists) is closed.
180The
fea681da
MK
181.I mode
182argument is used just as in the
e511ffb6 183.BR fopen ()
c13182ef 184function.
847e0d88 185.PP
7eed8a8f 186If the
af500011 187.I pathname
7eed8a8f
MK
188argument is a null pointer,
189.BR freopen ()
190changes the mode of the stream to that specified in
191.IR mode ;
192that is,
193.BR freopen ()
194reopens the pathname that is associated with the stream.
195The specification for this behavior was added in the C99 standard, which says:
847e0d88 196.PP
7eed8a8f
MK
197.RS
198In this case,
199the file descriptor associated with the stream need not be closed
200if the call to
201.BR freopen ()
202succeeds.
203It is implementation-defined which changes of mode are permitted (if any),
204and under what circumstances.
205.RE
206.PP
c13182ef 207The primary use of the
e511ffb6 208.BR freopen ()
fea681da 209function is to change the file associated with a standard text stream
b9d200ce 210.RI ( stderr ", " stdin ", or " stdout ).
47297adb 211.SH RETURN VALUE
fea681da 212Upon successful completion
e511ffb6 213.BR fopen (),
d556548b 214.BR fdopen (),
fea681da 215and
e511ffb6 216.BR freopen ()
fea681da 217return a
836f07c1 218.I FILE
c13182ef 219pointer.
28d03ce9 220Otherwise, NULL is returned and
fea681da
MK
221.I errno
222is set to indicate the error.
223.SH ERRORS
224.TP
225.B EINVAL
226The
227.I mode
228provided to
e511ffb6
MK
229.BR fopen (),
230.BR fdopen (),
fea681da 231or
e511ffb6 232.BR freopen ()
fea681da
MK
233was invalid.
234.PP
235The
e511ffb6 236.BR fopen (),
d556548b 237.BR fdopen (),
fea681da 238and
e511ffb6 239.BR freopen ()
fea681da
MK
240functions may also fail and set
241.I errno
242for any of the errors specified for the routine
243.BR malloc (3).
244.PP
245The
e511ffb6 246.BR fopen ()
fea681da
MK
247function may also fail and set
248.I errno
249for any of the errors specified for the routine
250.BR open (2).
251.PP
252The
e511ffb6 253.BR fdopen ()
fea681da
MK
254function may also fail and set
255.I errno
256for any of the errors specified for the routine
257.BR fcntl (2).
258.PP
259The
e511ffb6 260.BR freopen ()
fea681da
MK
261function may also fail and set
262.I errno
263for any of the errors specified for the routines
264.BR open (2),
9af134cd 265.BR fclose (3),
fea681da
MK
266and
267.BR fflush (3).
418ee2e8
MS
268.SH ATTRIBUTES
269For an explanation of the terms used in this section, see
270.BR attributes (7).
c466875e
MK
271.ad l
272.nh
418ee2e8
MS
273.TS
274allbox;
c466875e 275lbx lb lb
418ee2e8
MS
276l l l.
277Interface Attribute Value
278T{
279.BR fopen (),
280.BR fdopen (),
281.BR freopen ()
282T} Thread safety MT-Safe
283.TE
c466875e
MK
284.hy
285.ad
286.sp 1
47297adb 287.SH CONFORMING TO
bb7091c0
MK
288.BR fopen (),
289.BR freopen ():
290POSIX.1-2001, POSIX.1-2008, C89, C99.
847e0d88 291.PP
bb7091c0
MK
292.BR fdopen ():
293POSIX.1-2001, POSIX.1-2008.
d597239c 294.SH NOTES
c634028a 295.SS Glibc notes
3ed4fcbd 296The GNU C library allows the following extensions for the string specified in
a30e090e
MK
297.IR mode :
298.TP
bcd70adc 299.BR c " (since glibc 2.3.3)"
c13182ef 300Do not make the open operation,
bcd70adc 301or subsequent read and write operations,
1f08fc80 302thread cancelation points.
78a9ad25
MK
303This flag is ignored for
304.BR fdopen ().
bcd70adc 305.TP
d8d79fa2
MK
306.BR e " (since glibc 2.7)"
307Open the file with the
308.B O_CLOEXEC
309flag.
310See
311.BR open (2)
312for more information.
78a9ad25
MK
313This flag is ignored for
314.BR fdopen ().
d8d79fa2 315.TP
bcd70adc 316.BR m " (since glibc 2.3)"
c13182ef 317Attempt to access the file using
bcd70adc 318.BR mmap (2),
c13182ef 319rather than I/O system calls
bcd70adc
MK
320.RB ( read (2),
321.BR write (2)).
c13182ef 322Currently,
bcd70adc 323.\" As at glibc 2.4:
2d2c82e1 324use of
bcd70adc 325.BR mmap (2)
33a0ccb2 326is attempted only for a file opened for reading.
bcd70adc 327.TP
a30e090e 328.B x
accdf645 329.\" Since glibc 2.0?
bea08fec 330.\" FIXME . C11 specifies this flag
a30e090e 331Open the file exclusively
c13182ef 332(like the
a30e090e
MK
333.B O_EXCL
334flag of
335.BR open (2)).
336If the file already exists,
337.BR fopen ()
338fails, and sets
339.I errno
340to
341.BR EEXIST .
bcd70adc
MK
342This flag is ignored for
343.BR fdopen ().
d33e8466
MK
344.PP
345In addition to the above characters,
346.BR fopen ()
347and
348.BR freopen ()
afd6f6c5 349support the following syntax
d33e8466
MK
350in
351.IR mode :
847e0d88 352.PP
d33e8466 353.BI " ,ccs=" string
847e0d88 354.PP
d33e8466
MK
355The given
356.I string
357is taken as the name of a coded character set and
358the stream is marked as wide-oriented.
359Thereafter, internal conversion functions convert I/O
360to and from the character set
361.IR string .
362If the
363.BI ,ccs= string
364syntax is not specified,
365then the wide-orientation of the stream is
366determined by the first file operation.
afd6f6c5 367If that operation is a wide-character operation,
d33e8466
MK
368the stream is marked wide-oriented,
369and functions to convert to the coded character set are loaded.
4d9edb6e 370.SH BUGS
afd6f6c5
MK
371When parsing for individual flag characters in
372.IR mode
6f0f6cf6 373(i.e., the characters preceding the "ccs" specification),
afd6f6c5 374the glibc implementation of
bea08fec 375.\" FIXME . http://sourceware.org/bugzilla/show_bug.cgi?id=12685
4d9edb6e
MK
376.BR fopen ()
377and
378.BR freopen ()
379limits the number of characters examined in
380.I mode
381to 7 (or, in glibc versions before 2.14, to 6,
382which was not enough to include possible specifications such as "rb+cmxe").
383The current implementation of
384.BR fdopen ()
6f0f6cf6 385parses at most 5 characters in
4d9edb6e 386.IR mode .
47297adb 387.SH SEE ALSO
fea681da
MK
388.BR open (2),
389.BR fclose (3),
5f0aa64a
PB
390.BR fileno (3),
391.BR fmemopen (3),
a6bd38c6
MK
392.BR fopencookie (3),
393.BR open_memstream (3)