2004-11-03 13:51:07 +00:00
|
|
|
.\" Copyright (c) 1990, 1991 The Regents of the University of California.
|
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" This code is derived from software contributed to Berkeley by
|
|
|
|
.\" Chris Torek and the American National Standards Committee X3,
|
|
|
|
.\" on Information Processing Systems.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\" 3. All advertising materials mentioning features or use of this software
|
|
|
|
.\" must display the following acknowledgement:
|
|
|
|
.\" This product includes software developed by the University of
|
|
|
|
.\" California, Berkeley and its contributors.
|
|
|
|
.\" 4. Neither the name of the University nor the names of its contributors
|
|
|
|
.\" may be used to endorse or promote products derived from this software
|
|
|
|
.\" without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
|
|
|
.\" @(#)fopen.3 6.8 (Berkeley) 6/29/91
|
|
|
|
.\"
|
|
|
|
.\" Converted for Linux, Mon Nov 29 15:22:01 1993, faith@cs.unc.edu
|
|
|
|
.\" Modified, aeb, 960421, 970806
|
|
|
|
.\" Modified, joey, aeb, 2002-01-03
|
|
|
|
.\"
|
2009-02-23 02:34:12 +00:00
|
|
|
.TH FOPEN 3 2009-02-23 "GNU" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
|
|
|
fopen, fdopen, freopen \- stream open functions
|
|
|
|
.SH SYNOPSIS
|
2007-12-23 13:45:24 +00:00
|
|
|
.nf
|
2004-11-03 13:51:07 +00:00
|
|
|
.B #include <stdio.h>
|
|
|
|
.sp
|
|
|
|
.BI "FILE *fopen(const char *" path ", const char *" mode );
|
2007-12-23 13:45:24 +00:00
|
|
|
|
2007-12-24 22:04:47 +00:00
|
|
|
.BI "FILE *fdopen(int " fd ", const char *" mode );
|
2007-12-23 13:45:24 +00:00
|
|
|
|
2004-11-03 13:51:07 +00:00
|
|
|
.BI "FILE *freopen(const char *" path ", const char *" mode ", FILE *" stream );
|
2007-12-23 13:45:24 +00:00
|
|
|
.fi
|
2007-07-08 12:11:40 +00:00
|
|
|
.sp
|
|
|
|
.in -4n
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
|
|
.BR feature_test_macros (7)):
|
|
|
|
.in
|
|
|
|
.sp
|
|
|
|
.BR fdopen ():
|
2008-08-29 15:50:13 +00:00
|
|
|
_POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH DESCRIPTION
|
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR fopen ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function opens the file whose name is the string pointed to by
|
|
|
|
.I path
|
|
|
|
and associates a stream with it.
|
|
|
|
.PP
|
|
|
|
The argument
|
|
|
|
.I mode
|
|
|
|
points to a string beginning with one of the following sequences
|
|
|
|
(Additional characters may follow these sequences.):
|
|
|
|
.TP
|
|
|
|
.B r
|
2007-04-12 22:42:49 +00:00
|
|
|
Open text file for reading.
|
|
|
|
The stream is positioned at the beginning of the file.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B r+
|
2007-04-12 22:42:49 +00:00
|
|
|
Open for reading and writing.
|
|
|
|
The stream is positioned at the beginning of the file.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B w
|
2007-04-12 22:42:49 +00:00
|
|
|
Truncate file to zero length or create text file for writing.
|
|
|
|
The stream is positioned at the beginning of the file.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B w+
|
2007-04-12 22:42:49 +00:00
|
|
|
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
|
2004-11-03 13:51:07 +00:00
|
|
|
the file.
|
|
|
|
.TP
|
|
|
|
.B a
|
2007-04-12 22:42:49 +00:00
|
|
|
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.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B a+
|
2007-04-12 22:42:49 +00:00
|
|
|
Open for reading and appending (writing at end of file).
|
|
|
|
The file is created if it does not exist.
|
2005-10-04 12:35:17 +00:00
|
|
|
The initial file position for reading is at the beginning of the file,
|
|
|
|
but output is always appended to the end of the file.
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
|
|
|
The
|
|
|
|
.I mode
|
2008-06-09 15:49:35 +00:00
|
|
|
string can also include the letter \(aqb\(aq either as a last character or as
|
2004-11-03 13:51:07 +00:00
|
|
|
a character between the characters in any of the two-character strings
|
2007-04-12 22:42:49 +00:00
|
|
|
described above.
|
|
|
|
This is strictly for compatibility with C89
|
2008-06-09 15:49:35 +00:00
|
|
|
and has no effect; the \(aqb\(aq is ignored on all POSIX
|
2004-11-03 13:51:07 +00:00
|
|
|
conforming systems, including Linux.
|
|
|
|
(Other systems may treat text files and binary files differently,
|
2008-06-09 15:49:35 +00:00
|
|
|
and adding the \(aqb\(aq may be a good idea if you do I/O to a binary
|
intro.1, time.1, accept.2, bind.2, connect.2, execve.2, flock.2, getdents.2, getpriority.2, getuid.2, intro.2, ioctl.2, mincore.2, mknod.2, personality.2, ptrace.2, read.2, recv.2, select_tut.2, send.2, sendfile.2, shmctl.2, sigaction.2, signal.2, stat.2, times.2, truncate.2, umask.2, wait.2, MB_CUR_MAX.3, MB_LEN_MAX.3, argz_add.3, btowc.3, clearenv.3, clock.3, cmsg.3, end.3, endian.3, errno.3, exit.3, fgetwc.3, fgetws.3, fopen.3, fputwc.3, fputws.3, fseek.3, fwide.3, getfsent.3, getgrnam.3, gethostid.3, getipnodebyname.3, getmntent.3, getpwnam.3, getwchar.3, grantpt.3, iconv.3, iconv_close.3, iconv_open.3, insque.3, intro.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, malloc.3, mblen.3, mbrlen.3, mbrtowc.3, mbsinit.3, mbsnrtowcs.3, mbsrtowcs.3, mbstowcs.3, mbtowc.3, mkstemp.3, mktemp.3, nl_langinfo.3, openpty.3, posix_openpt.3, printf.3, ptsname.3, putwchar.3, qecvt.3, rcmd.3, readdir.3, rexec.3, rpc.3, setnetgrent.3, shm_open.3, sigpause.3, stdin.3, stpcpy.3, strftime.3, strptime.3, syslog.3, towctrans.3, towlower.3, towupper.3, ttyslot.3, ungetwc.3, unlocked_stdio.3, wcpcpy.3, wcpncpy.3, wcrtomb.3, wcscasecmp.3, wcscat.3, wcschr.3, wcscmp.3, wcscpy.3, wcscspn.3, wcsdup.3, wcslen.3, wcsncasecmp.3, wcsncat.3, wcsncmp.3, wcsncpy.3, wcsnlen.3, wcsnrtombs.3, wcspbrk.3, wcsrchr.3, wcsrtombs.3, wcsspn.3, wcsstr.3, wcstok.3, wcstombs.3, wcswidth.3, wctob.3, wctomb.3, wctrans.3, wctype.3, wcwidth.3, wmemchr.3, wmemcmp.3, wmemcpy.3, wmemmove.3, wmemset.3, wprintf.3, console_ioctl.4, pts.4, elf.5, filesystems.5, hosts.5, proc.5, ttytype.5, boot.7, capabilities.7, credentials.7, epoll.7, glob.7, koi8-r.7, path_resolution.7, pty.7, signal.7, suffixes.7, time.7, unicode.7, unix.7, uri.7, utf-8.7: global fix: s/Unix/UNIX/
The man pages were rather inconsistent in the use of "Unix"
versus "UNIX". Let's go with the trademark usage.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-10-12 04:45:38 +00:00
|
|
|
file and expect that your program may be ported to non-UNIX
|
2004-11-03 13:51:07 +00:00
|
|
|
environments.)
|
|
|
|
.PP
|
2008-10-12 06:08:50 +00:00
|
|
|
See NOTES below for details of glibc extensions for
|
|
|
|
.IR mode .
|
|
|
|
.PP
|
2004-11-03 13:51:07 +00:00
|
|
|
Any created files will have mode
|
2007-07-09 20:43:38 +00:00
|
|
|
.BR S_IRUSR " | " S_IWUSR " | " S_IRGRP " | " S_IWGRP " | " S_IROTH " | " S_IWOTH
|
2007-06-11 19:20:30 +00:00
|
|
|
(0666), as modified by the process's umask value (see
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR umask (2)).
|
|
|
|
.PP
|
|
|
|
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
|
2007-05-12 00:30:29 +00:00
|
|
|
.BR fseek (3)
|
2004-11-03 13:51:07 +00:00
|
|
|
or
|
2007-05-12 00:30:29 +00:00
|
|
|
.BR fgetpos (3)
|
2007-04-12 22:42:49 +00:00
|
|
|
operation between write and read operations on such a stream.
|
2007-06-22 19:42:52 +00:00
|
|
|
This operation may be an apparent no-op
|
2007-07-18 20:24:30 +00:00
|
|
|
(as in \fIfseek(..., 0L, SEEK_CUR)\fP
|
2007-06-22 19:42:52 +00:00
|
|
|
called for its synchronizing side effect.
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
2007-07-18 20:24:30 +00:00
|
|
|
Opening a file in append mode (\fBa\fP as the first character of
|
2004-11-03 13:51:07 +00:00
|
|
|
.IR mode )
|
|
|
|
causes all subsequent write operations to this stream to occur
|
|
|
|
at end-of-file, as if preceded by an
|
2007-07-09 20:43:38 +00:00
|
|
|
.nf
|
|
|
|
|
|
|
|
fseek(stream,0,SEEK_END);
|
|
|
|
.fi
|
|
|
|
.PP
|
2004-11-03 13:51:07 +00:00
|
|
|
call.
|
|
|
|
.PP
|
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR fdopen ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function associates a stream with the existing file descriptor,
|
2007-12-24 22:04:47 +00:00
|
|
|
.IR fd .
|
2004-11-03 13:51:07 +00:00
|
|
|
The
|
|
|
|
.I mode
|
|
|
|
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
|
2007-12-24 22:04:47 +00:00
|
|
|
.IR fd ,
|
2004-11-03 13:51:07 +00:00
|
|
|
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
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR fdopen ()
|
2004-11-03 13:51:07 +00:00
|
|
|
is closed.
|
|
|
|
The result of applying
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR fdopen ()
|
2004-11-03 13:51:07 +00:00
|
|
|
to a shared memory object is undefined.
|
|
|
|
.PP
|
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR freopen ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function opens the file whose name is the string pointed to by
|
|
|
|
.I path
|
|
|
|
and associates the stream pointed to by
|
|
|
|
.I stream
|
2007-04-12 22:42:49 +00:00
|
|
|
with it.
|
|
|
|
The original stream (if it exists) is closed.
|
|
|
|
The
|
2004-11-03 13:51:07 +00:00
|
|
|
.I mode
|
|
|
|
argument is used just as in the
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR fopen ()
|
2007-04-12 22:42:49 +00:00
|
|
|
function.
|
|
|
|
The primary use of the
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR freopen ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function is to change the file associated with a standard text stream
|
2007-10-03 06:40:03 +00:00
|
|
|
.RI ( stderr ", " stdin ", or " stdout ).
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "RETURN VALUE"
|
|
|
|
Upon successful completion
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR fopen (),
|
|
|
|
.BR fdopen ()
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR freopen ()
|
2004-11-03 13:51:07 +00:00
|
|
|
return a
|
2005-11-02 11:55:24 +00:00
|
|
|
.I FILE
|
2007-04-12 22:42:49 +00:00
|
|
|
pointer.
|
2009-02-23 02:34:12 +00:00
|
|
|
Otherwise, NULL is returned and
|
2004-11-03 13:51:07 +00:00
|
|
|
.I errno
|
|
|
|
is set to indicate the error.
|
|
|
|
.SH ERRORS
|
|
|
|
.TP
|
|
|
|
.B EINVAL
|
|
|
|
The
|
|
|
|
.I mode
|
|
|
|
provided to
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR fopen (),
|
|
|
|
.BR fdopen (),
|
2004-11-03 13:51:07 +00:00
|
|
|
or
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR freopen ()
|
2004-11-03 13:51:07 +00:00
|
|
|
was invalid.
|
|
|
|
.PP
|
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR fopen (),
|
|
|
|
.BR fdopen ()
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR freopen ()
|
2004-11-03 13:51:07 +00:00
|
|
|
functions may also fail and set
|
|
|
|
.I errno
|
|
|
|
for any of the errors specified for the routine
|
|
|
|
.BR malloc (3).
|
|
|
|
.PP
|
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR fopen ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function may also fail and set
|
|
|
|
.I errno
|
|
|
|
for any of the errors specified for the routine
|
|
|
|
.BR open (2).
|
|
|
|
.PP
|
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR fdopen ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function may also fail and set
|
|
|
|
.I errno
|
|
|
|
for any of the errors specified for the routine
|
|
|
|
.BR fcntl (2).
|
|
|
|
.PP
|
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR freopen ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function may also fail and set
|
|
|
|
.I errno
|
|
|
|
for any of the errors specified for the routines
|
|
|
|
.BR open (2),
|
|
|
|
.BR fclose (3)
|
|
|
|
and
|
|
|
|
.BR fflush (3).
|
|
|
|
.SH "CONFORMING TO"
|
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR fopen ()
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR freopen ()
|
2007-04-12 22:42:49 +00:00
|
|
|
functions conform to C89.
|
2006-12-16 22:11:08 +00:00
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR fdopen ()
|
2006-08-03 13:57:30 +00:00
|
|
|
function conforms to POSIX.1-1990.
|
2007-05-18 10:30:02 +00:00
|
|
|
.SH NOTES
|
|
|
|
.SS Glibc Notes
|
2006-05-13 21:52:39 +00:00
|
|
|
The GNU C library allows the following extensions for the string specified in
|
2005-12-15 15:58:16 +00:00
|
|
|
.IR mode :
|
|
|
|
.TP
|
2006-05-03 23:52:08 +00:00
|
|
|
.BR c " (since glibc 2.3.3)"
|
2007-04-12 22:42:49 +00:00
|
|
|
Do not make the open operation,
|
2006-05-03 23:52:08 +00:00
|
|
|
or subsequent read and write operations,
|
|
|
|
thread cancellation points.
|
|
|
|
.TP
|
2007-11-19 00:51:38 +00:00
|
|
|
.BR e " (since glibc 2.7)"
|
|
|
|
Open the file with the
|
|
|
|
.B O_CLOEXEC
|
|
|
|
flag.
|
|
|
|
See
|
|
|
|
.BR open (2)
|
|
|
|
for more information.
|
|
|
|
.TP
|
2006-05-03 23:52:08 +00:00
|
|
|
.BR m " (since glibc 2.3)"
|
2007-04-12 22:42:49 +00:00
|
|
|
Attempt to access the file using
|
2006-05-03 23:52:08 +00:00
|
|
|
.BR mmap (2),
|
2007-04-12 22:42:49 +00:00
|
|
|
rather than I/O system calls
|
2006-05-03 23:52:08 +00:00
|
|
|
.RB ( read (2),
|
|
|
|
.BR write (2)).
|
2007-04-12 22:42:49 +00:00
|
|
|
Currently,
|
2006-05-03 23:52:08 +00:00
|
|
|
.\" As at glibc 2.4:
|
2006-05-08 21:27:27 +00:00
|
|
|
use of
|
2006-05-03 23:52:08 +00:00
|
|
|
.BR mmap (2)
|
2006-05-08 21:27:27 +00:00
|
|
|
is only attempted for a file opened for reading.
|
2006-05-03 23:52:08 +00:00
|
|
|
.TP
|
2005-12-15 15:58:16 +00:00
|
|
|
.B x
|
2006-05-25 21:40:52 +00:00
|
|
|
.\" Since glibc 2.0?
|
2005-12-15 15:58:16 +00:00
|
|
|
Open the file exclusively
|
2007-04-12 22:42:49 +00:00
|
|
|
(like the
|
2005-12-15 15:58:16 +00:00
|
|
|
.B O_EXCL
|
|
|
|
flag of
|
|
|
|
.BR open (2)).
|
|
|
|
If the file already exists,
|
|
|
|
.BR fopen ()
|
|
|
|
fails, and sets
|
|
|
|
.I errno
|
|
|
|
to
|
|
|
|
.BR EEXIST .
|
2006-05-03 23:52:08 +00:00
|
|
|
This flag is ignored for
|
|
|
|
.BR fdopen ().
|
2006-03-23 02:13:08 +00:00
|
|
|
.\" FIXME document /,ccs= charset/
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR open (2),
|
|
|
|
.BR fclose (3),
|
2008-12-05 16:30:22 +00:00
|
|
|
.BR fileno (3),
|
|
|
|
.BR fmemopen (3),
|
|
|
|
.BR fopencookie (3)
|