4.3BSD/usr/man/man3/fopen.3s

.\" Copyright (c) 1980 Regents of the University of California.
.\" All rights reserved.  The Berkeley software License Agreement
.\" specifies the terms and conditions for redistribution.
.\"
.\"	@(#)fopen.3s	6.3 (Berkeley) 5/27/86
.\"
.TH FOPEN 3S  "May 27, 1986"
.UC 4
.SH NAME
fopen, freopen, fdopen \- open a stream
.SH SYNOPSIS
.B #include <stdio.h>
.PP
.SM
.B FILE
.B *fopen(filename, type)
.br
.B char *filename, *type;
.PP
.SM
.B FILE
.B *freopen(filename, type, stream)
.br
.B char *filename, *type;
.br
.SM
.B FILE
.B *stream;
.PP
.SM
.B FILE
.B *fdopen(fildes, type)
.br
.B char *type;
.SH DESCRIPTION
.I Fopen
opens the file named by
.I filename
and associates a stream with it.
.I Fopen
returns a pointer to be used to identify the stream in subsequent operations.
.PP
.I Type
is a character string having one of the following values:
.TP 5
"r"
open for reading
.ns
.TP 5
"w"
create for writing
.ns
.TP 5
"a"
append: open for writing at end of file, or create for writing
.PP
In addition, each
.I type
may be followed by a "+" to have the file opened for reading and writing.
"r+" positions the stream at the beginning of the file, "w+" creates
or truncates it, and "a+" positions it at the end.  Both reads and writes
may be used on read/write streams, with the limitation that an
.I fseek, rewind,
or reading an end-of-file must be used between a read and a write or vice-versa.
.PP
.I Freopen
substitutes the named file in place of the open
.IR stream .
It returns the original value of
.IR stream .
The original stream is closed.
.PP
.I Freopen
is typically used to attach the preopened constant names,
.B stdin, stdout, stderr,
to specified files.
.PP
.I Fdopen
associates a stream with a file descriptor obtained from
.I open, dup, creat,
or
.IR pipe (2).
The
.I type
of the stream must agree with the mode of the open file.
.SH "SEE ALSO"
open(2),
fclose(3)
.SH DIAGNOSTICS
.I Fopen
and 
.I freopen
return the pointer
.SM
.B NULL
if
.I filename
cannot be accessed,
if too many files are already open,
or if other resources needed cannot be allocated.
.SH BUGS
.I Fdopen
is not portable to systems other than UNIX.
.PP
The read/write 
.I types
do not exist on all systems.  Those systems without
read/write modes will probably treat the 
.I type
as if the "+" was not present.  These are unreliable in any event.
.PP
In order to support the same number of open files as does the system,
.I fopen
must allocate additional memory for data structures using
.I calloc
after 20 files have been opened.
This confuses some programs which use their own memory allocators.
An undocumented routine,
.IR f_prealloc ,
may be called to force immediate allocation of all internal memory
except for buffers.