'\"macro stdmacro
.if n .pH g3.fopen @(#)fopen	40.6 of 9/18/89
.\" Copyright (c) 1988 Sun Microsystems, Inc. - All Rights Reserved.
.\"
.nr X
.if \nX=0 .ds x} fopen 3S "BSD Compatibility Package" "\&"
.if \nX=1 .ds x} fopen 3S "BSD Compatibility Package"
.if \nX=2 .ds x} fopen 3S "" "\&"
.if \nX=3 .ds x} fopen "" "" "\&"
.TH \*(x}
.SH NAME
\f4fopen\f1, \f4freopen\f1, \f4fdopen\f1 \- open a stream
.SH SYNOPSIS
.nf
\f4cc \f1[ \f2flag\f1... ] \f2file\f1 ... \f4\-lucb\f1
.P
\f4#include <stdio.h>\f1
.P
\f4FILE *fopen(filename, type)\f1
\f4char *filename, *type;\f1
.P
\f4FILE *freopen(filename, type, stream)\f1
\f4char *filename, *type;\f1
\f4FILE *stream;\f1
.P
\f4FILE *fdopen(fildes, type)\f1
\f4int fildes;\f1
\f4char *type;\f1
.fi
.SH DESCRIPTION
.P
\f4fopen\f1
opens the file named by
.I filename
and associates a stream with it.
If the open succeeds,
\f4fopen\f1
returns a pointer to be used to identify the stream in subsequent operations.
.P
.I filename
points to a character string that contains
the name of the file to be opened.
.P
.I type
is a character string having one of the following values:
.RS
.TP 5
\f4r\f1
open for reading
.ns
.TP
\f4w\f1
truncate or create for writing
.ns
.TP
\f4a\f1
append: open for writing at end of file, or create for writing
.TP
\f4r+\f1
open for update (reading and writing)
.ns
.TP
\f4w+\f1
truncate or create for update
.ns
.TP
\f4a+\f1
append; open or create for update at
.SM EOF
.RE
.P
\f4freopen\f1
opens the file named by
.I filename
and associates the stream pointed to by
.I stream
with it.
The
.I type
argument is used just as in
\f4fopen\f1.
The original stream is closed, regardless of whether the open
ultimately succeeds.
If the open succeeds,
\f4freopen\f1
returns the original value of
.IR stream .
.P
\f4freopen\f1
is typically used to attach the preopened
streams associated with
\f4stdin\f1,
\f4stdout\f1,
and
\f4stderr\f1
to other files.
.P
\f4fdopen\f1
associates a stream with the file descriptor
.IR fildes .
File descriptors are obtained from calls like
\f4open\f1,
\f4dup\f1,
\f4creat\f1,
or
\f4pipe\f1(2),
which open files but do not return streams.
Streams are necessary input for many of the Section 3S library routines.
The
.I type
of the stream must agree with the mode of the open file.
.P
When a file is opened for update, both input and output may be
done on the resulting stream.
However, output may not be directly followed by input without an
intervening
\f4fseek\f1
or
\f4rewind\f1,
and input may not be directly followed by output without an
intervening
\f4fseek\f1,
\f4rewind\f1,
or an input operation which encounters 
.SM EOF\s0.
.SH "SEE ALSO"
\f4open\fP(2),
\f4pipe\fP(2),
\f4fclose\fP(3S),
\f4fseek\fP(3S),
\f4fopen\fP(3S),
\f4malloc\fP(3C)
in the \f2Programmer's Reference Manual\f1.
.SH "RETURN VALUE"
\f4fopen\f1,
\f4freopen\f1,
and
\f4fdopen\f1
return a
\f4NULL\fP
pointer on failure.
.SH NOTES
\f4fopen\f1 differs from the library routine of the same name in
the base system only in interface.
.P
In order to support the same number of open files that the system does,
\f4fopen\f1
must allocate additional memory for data structures using
\f4calloc\f1
[see \f4malloc\f1(3)]
after 64 files have been opened.
This confuses some programs which use their own memory allocators.
