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