581 lines
13 KiB
HTML
581 lines
13 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of FOPEN</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>FOPEN</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-05-09<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
fopen, fdopen, freopen - stream open functions
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>></B>
|
|
|
|
<B>FILE *fopen(const char *</B><I>pathname</I><B>, const char *</B><I>mode</I><B>);</B>
|
|
|
|
<B>FILE *fdopen(int </B><I>fd</I><B>, const char *</B><I>mode</I><B>);</B>
|
|
|
|
<B>FILE *freopen(const char *</B><I>pathname</I><B>, const char *</B><I>mode</I><B>, FILE *</B><I>stream</I><B>);</B>
|
|
</PRE>
|
|
|
|
<P>
|
|
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
|
|
|
|
|
|
<P>
|
|
|
|
<B>fdopen</B>():
|
|
|
|
_POSIX_C_SOURCE
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>fopen</B>()
|
|
|
|
function opens the file whose name is the string pointed to by
|
|
<I>pathname</I>
|
|
|
|
and associates a stream with it.
|
|
<P>
|
|
|
|
The argument
|
|
<I>mode</I>
|
|
|
|
points to a string beginning with one of the following sequences
|
|
(possibly followed by additional characters, as described below):
|
|
<DL COMPACT>
|
|
<DT id="1"><B>r</B>
|
|
|
|
<DD>
|
|
Open text file for reading.
|
|
The stream is positioned at the beginning of the file.
|
|
<DT id="2"><B>r+</B>
|
|
|
|
<DD>
|
|
Open for reading and writing.
|
|
The stream is positioned at the beginning of the file.
|
|
<DT id="3"><B>w</B>
|
|
|
|
<DD>
|
|
Truncate file to zero length or create text file for writing.
|
|
The stream is positioned at the beginning of the file.
|
|
<DT id="4"><B>w+</B>
|
|
|
|
<DD>
|
|
Open for reading and writing.
|
|
The file is created if it does not exist, otherwise it is truncated.
|
|
The stream is positioned at the beginning of
|
|
the file.
|
|
<DT id="5"><B>a</B>
|
|
|
|
<DD>
|
|
Open for appending (writing at end of file).
|
|
The file is created if it does not exist.
|
|
The stream is positioned at the end of the file.
|
|
<DT id="6"><B>a+</B>
|
|
|
|
<DD>
|
|
Open for reading and appending (writing at end of file).
|
|
The file is created if it does not exist.
|
|
Output is always appended to the end of the file.
|
|
POSIX is silent on what the initial read position is when using this mode.
|
|
For glibc, the initial file position for reading is at
|
|
the beginning of the file, but for Android/BSD/MacOS, the
|
|
initial file position for reading is at the end of the file.
|
|
</DL>
|
|
<P>
|
|
|
|
The
|
|
<I>mode</I>
|
|
|
|
string can also include the letter 'b' either as a last character or as
|
|
a character between the characters in any of the two-character strings
|
|
described above.
|
|
This is strictly for compatibility with C89
|
|
and has no effect; the 'b' is ignored on all POSIX
|
|
conforming systems, including Linux.
|
|
(Other systems may treat text files and binary files differently,
|
|
and adding the 'b' may be a good idea if you do I/O to a binary
|
|
file and expect that your program may be ported to non-UNIX
|
|
environments.)
|
|
<P>
|
|
|
|
See NOTES below for details of glibc extensions for
|
|
<I>mode</I>.
|
|
|
|
<P>
|
|
|
|
Any created file will have the mode
|
|
<B>S_IRUSR</B> | <B>S_IWUSR</B> | <B>S_IRGRP</B> | <B>S_IWGRP</B> | <B>S_IROTH</B> | <B>S_IWOTH</B>
|
|
|
|
(0666), as modified by the process's umask value (see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+umask">umask</A></B>(2)).
|
|
|
|
<P>
|
|
|
|
Reads and writes may be intermixed on read/write streams in any order.
|
|
Note that ANSI C requires that a file positioning function intervene
|
|
between output and input, unless an input operation encounters end-of-file.
|
|
(If this condition is not met, then a read is allowed to return the
|
|
result of writes other than the most recent.)
|
|
Therefore it is good practice (and indeed sometimes necessary
|
|
under Linux) to put an
|
|
<B><A HREF="/cgi-bin/man/man2html?3+fseek">fseek</A></B>(3)
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?3+fgetpos">fgetpos</A></B>(3)
|
|
|
|
operation between write and read operations on such a stream.
|
|
This operation may be an apparent no-op
|
|
(as in <I>fseek(..., 0L, SEEK_CUR)</I>
|
|
called for its synchronizing side effect).
|
|
<P>
|
|
|
|
Opening a file in append mode (<B>a</B> as the first character of
|
|
<I>mode</I>)
|
|
|
|
causes all subsequent write operations to this stream to occur
|
|
at end-of-file, as if preceded the call:
|
|
<P>
|
|
|
|
|
|
|
|
fseek(stream, 0, SEEK_END);
|
|
|
|
|
|
<P>
|
|
|
|
The file descriptor associated with the stream is opened as if by a call to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
|
|
|
|
with the following flags:
|
|
<DL COMPACT><DT id="7"><DD>
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>fopen() mode</B></TD><TD><B>open() flags</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD ALIGN=center><I>r</I></TD><TD>O_RDONLY<BR></TD></TR>
|
|
<TR VALIGN=top><TD ALIGN=center><I>w</I></TD><TD>O_WRONLY | O_CREAT | O_TRUNC<BR></TD></TR>
|
|
<TR VALIGN=top><TD ALIGN=center><I>a</I></TD><TD>O_WRONLY | O_CREAT | O_APPEND<BR></TD></TR>
|
|
<TR VALIGN=top><TD ALIGN=center><I>r+</I></TD><TD>O_RDWR<BR></TD></TR>
|
|
<TR VALIGN=top><TD ALIGN=center><I>w+</I></TD><TD>O_RDWR | O_CREAT | O_TRUNC<BR></TD></TR>
|
|
<TR VALIGN=top><TD ALIGN=center><I>a+</I></TD><TD>O_RDWR | O_CREAT | O_APPEND<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
</DL>
|
|
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H3>fdopen()</H3>
|
|
|
|
The
|
|
<B>fdopen</B>()
|
|
|
|
function associates a stream with the existing file descriptor,
|
|
<I>fd</I>.
|
|
|
|
The
|
|
<I>mode</I>
|
|
|
|
of the stream (one of the values "r", "r+", "w", "w+", "a", "a+")
|
|
must be compatible with the mode of the file descriptor.
|
|
The file position indicator of the new stream is set to that
|
|
belonging to
|
|
<I>fd</I>,
|
|
|
|
and the error and end-of-file indicators are cleared.
|
|
Modes "w" or "w+" do not cause truncation of the file.
|
|
The file descriptor is not dup'ed, and will be closed when
|
|
the stream created by
|
|
<B>fdopen</B>()
|
|
|
|
is closed.
|
|
The result of applying
|
|
<B>fdopen</B>()
|
|
|
|
to a shared memory object is undefined.
|
|
|
|
<A NAME="lbAF"> </A>
|
|
<H3>freopen()</H3>
|
|
|
|
The
|
|
<B>freopen</B>()
|
|
|
|
function opens the file whose name is the string pointed to by
|
|
<I>pathname</I>
|
|
|
|
and associates the stream pointed to by
|
|
<I>stream</I>
|
|
|
|
with it.
|
|
The original stream (if it exists) is closed.
|
|
The
|
|
<I>mode</I>
|
|
|
|
argument is used just as in the
|
|
<B>fopen</B>()
|
|
|
|
function.
|
|
<P>
|
|
|
|
If the
|
|
<I>pathname</I>
|
|
|
|
argument is a null pointer,
|
|
<B>freopen</B>()
|
|
|
|
changes the mode of the stream to that specified in
|
|
<I>mode</I>;
|
|
|
|
that is,
|
|
<B>freopen</B>()
|
|
|
|
reopens the pathname that is associated with the stream.
|
|
The specification for this behavior was added in the C99 standard, which says:
|
|
<P>
|
|
|
|
<DL COMPACT><DT id="8"><DD>
|
|
In this case,
|
|
the file descriptor associated with the stream need not be closed
|
|
if the call to
|
|
<B>freopen</B>()
|
|
|
|
succeeds.
|
|
It is implementation-defined which changes of mode are permitted (if any),
|
|
and under what circumstances.
|
|
</DL>
|
|
|
|
<P>
|
|
|
|
The primary use of the
|
|
<B>freopen</B>()
|
|
|
|
function is to change the file associated with a standard text stream
|
|
(<I>stderr</I>, <I>stdin</I>, or <I>stdout</I>).
|
|
|
|
<A NAME="lbAG"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
Upon successful completion
|
|
<B>fopen</B>(),
|
|
|
|
<B>fdopen</B>()
|
|
|
|
and
|
|
<B>freopen</B>()
|
|
|
|
return a
|
|
<I>FILE</I>
|
|
|
|
pointer.
|
|
Otherwise, NULL is returned and
|
|
<I>errno</I>
|
|
|
|
is set to indicate the error.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="9"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
The
|
|
<I>mode</I>
|
|
|
|
provided to
|
|
<B>fopen</B>(),
|
|
|
|
<B>fdopen</B>(),
|
|
|
|
or
|
|
<B>freopen</B>()
|
|
|
|
was invalid.
|
|
</DL>
|
|
<P>
|
|
|
|
The
|
|
<B>fopen</B>(),
|
|
|
|
<B>fdopen</B>()
|
|
|
|
and
|
|
<B>freopen</B>()
|
|
|
|
functions may also fail and set
|
|
<I>errno</I>
|
|
|
|
for any of the errors specified for the routine
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3).
|
|
|
|
<P>
|
|
|
|
The
|
|
<B>fopen</B>()
|
|
|
|
function may also fail and set
|
|
<I>errno</I>
|
|
|
|
for any of the errors specified for the routine
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2).
|
|
|
|
<P>
|
|
|
|
The
|
|
<B>fdopen</B>()
|
|
|
|
function may also fail and set
|
|
<I>errno</I>
|
|
|
|
for any of the errors specified for the routine
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2).
|
|
|
|
<P>
|
|
|
|
The
|
|
<B>freopen</B>()
|
|
|
|
function may also fail and set
|
|
<I>errno</I>
|
|
|
|
for any of the errors specified for the routines
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+fclose">fclose</A></B>(3),
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?3+fflush">fflush</A></B>(3).
|
|
|
|
<A NAME="lbAI"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>fopen</B>(),
|
|
|
|
<B>fdopen</B>(),
|
|
|
|
<B>freopen</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
<B>fopen</B>(),
|
|
|
|
<B>freopen</B>():
|
|
|
|
POSIX.1-2001, POSIX.1-2008, C89, C99.
|
|
<P>
|
|
|
|
<B>fdopen</B>():
|
|
|
|
POSIX.1-2001, POSIX.1-2008.
|
|
<A NAME="lbAK"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H3>Glibc notes</H3>
|
|
|
|
The GNU C library allows the following extensions for the string specified in
|
|
<I>mode</I>:
|
|
|
|
<DL COMPACT>
|
|
<DT id="10"><B>c</B> (since glibc 2.3.3)
|
|
|
|
<DD>
|
|
Do not make the open operation,
|
|
or subsequent read and write operations,
|
|
thread cancellation points.
|
|
This flag is ignored for
|
|
<B>fdopen</B>().
|
|
|
|
<DT id="11"><B>e</B> (since glibc 2.7)
|
|
|
|
<DD>
|
|
Open the file with the
|
|
<B>O_CLOEXEC</B>
|
|
|
|
flag.
|
|
See
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
|
|
|
|
for more information.
|
|
This flag is ignored for
|
|
<B>fdopen</B>().
|
|
|
|
<DT id="12"><B>m</B> (since glibc 2.3)
|
|
|
|
<DD>
|
|
Attempt to access the file using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2),
|
|
|
|
rather than I/O system calls
|
|
(<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+write">write</A></B>(2)).
|
|
|
|
Currently,
|
|
|
|
use of
|
|
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)
|
|
|
|
is attempted only for a file opened for reading.
|
|
<DT id="13"><B>x</B>
|
|
|
|
<DD>
|
|
|
|
|
|
Open the file exclusively
|
|
(like the
|
|
<B>O_EXCL</B>
|
|
|
|
flag of
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)).
|
|
|
|
If the file already exists,
|
|
<B>fopen</B>()
|
|
|
|
fails, and sets
|
|
<I>errno</I>
|
|
|
|
to
|
|
<B>EEXIST</B>.
|
|
|
|
This flag is ignored for
|
|
<B>fdopen</B>().
|
|
|
|
</DL>
|
|
<P>
|
|
|
|
In addition to the above characters,
|
|
<B>fopen</B>()
|
|
|
|
and
|
|
<B>freopen</B>()
|
|
|
|
support the following syntax
|
|
in
|
|
<I>mode</I>:
|
|
|
|
<P>
|
|
|
|
<B> ,ccs=</B><I>string</I>
|
|
|
|
<P>
|
|
|
|
The given
|
|
<I>string</I>
|
|
|
|
is taken as the name of a coded character set and
|
|
the stream is marked as wide-oriented.
|
|
Thereafter, internal conversion functions convert I/O
|
|
to and from the character set
|
|
<I>string</I>.
|
|
|
|
If the
|
|
<B>,ccs=</B><I>string</I>
|
|
|
|
syntax is not specified,
|
|
then the wide-orientation of the stream is
|
|
determined by the first file operation.
|
|
If that operation is a wide-character operation,
|
|
the stream is marked wide-oriented,
|
|
and functions to convert to the coded character set are loaded.
|
|
<A NAME="lbAM"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
When parsing for individual flag characters in
|
|
<I>mode</I>
|
|
|
|
(i.e., the characters preceding the "ccs" specification),
|
|
the glibc implementation of
|
|
|
|
<B>fopen</B>()
|
|
|
|
and
|
|
<B>freopen</B>()
|
|
|
|
limits the number of characters examined in
|
|
<I>mode</I>
|
|
|
|
to 7 (or, in glibc versions before 2.14, to 6,
|
|
which was not enough to include possible specifications such as "rb+cmxe").
|
|
The current implementation of
|
|
<B>fdopen</B>()
|
|
|
|
parses at most 5 characters in
|
|
<I>mode</I>.
|
|
|
|
<A NAME="lbAN"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+fclose">fclose</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+fileno">fileno</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+fmemopen">fmemopen</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+fopencookie">fopencookie</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+open_memstream">open_memstream</A></B>(3)
|
|
|
|
<A NAME="lbAO"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="14"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="15"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="16"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="17"><A HREF="#lbAE">fdopen()</A><DD>
|
|
<DT id="18"><A HREF="#lbAF">freopen()</A><DD>
|
|
</DL>
|
|
<DT id="19"><A HREF="#lbAG">RETURN VALUE</A><DD>
|
|
<DT id="20"><A HREF="#lbAH">ERRORS</A><DD>
|
|
<DT id="21"><A HREF="#lbAI">ATTRIBUTES</A><DD>
|
|
<DT id="22"><A HREF="#lbAJ">CONFORMING TO</A><DD>
|
|
<DT id="23"><A HREF="#lbAK">NOTES</A><DD>
|
|
<DL>
|
|
<DT id="24"><A HREF="#lbAL">Glibc notes</A><DD>
|
|
</DL>
|
|
<DT id="25"><A HREF="#lbAM">BUGS</A><DD>
|
|
<DT id="26"><A HREF="#lbAN">SEE ALSO</A><DD>
|
|
<DT id="27"><A HREF="#lbAO">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:43 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|