FOPEN(S) XENIX System V FOPEN(S)
Name
fopen, freopen, fdopen - Opens a stream.
Syntax
#include <stdio.h>
FILE *fopen (filename, type)
char *filename, *type;
FILE *freopen (filename, type, stream)
char *filename, *type;
FILE *stream;
FILE *fdopen (fildes, type)
int fildes;
char *type;
Description
fopen opens the file named by filename and associates a
stream with it. fopen returns a pointer to be used to
identify the stream in subsequent operations.
type is a character string having one of the following
values:
r Open for reading
w Create for writing
a Append; open for writing at end of file, or create for
writing
r+ Open for update (reading and writing)
w+ Create for update
a+ Append; open or create for update at end of file
freopen substitutes the named file in place of the open
stream. It returns the original value of stream. The
original stream is closed, regardless of whether the open
call ultimately succeeds.
freopen is typically used to attach the preopened constant
names stdin, stdout, and stderr to specified files.
Page 1 (printed 8/7/87)
FOPEN(S) XENIX System V FOPEN(S)
fdopen associates a stream with a file descriptor obtained
from open, dup, creat, or pipe(S). The type of the stream
must agree with the mode of the open file. The type must be
provided because the standard I/O library has no way to
query the type of an open file descriptor. fdopen returns
the new stream.
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 fseek or
rewind, and input may not be directly followed by output
without an intervening fseek, rewind, or an input operation
which encounters the end of the file.
When a file is opened for append (that is, when type is
``a'' or ``a+''), it is impossible to overwrite information
already in the file. fseek may be used to reposition the
file pointer to any position in the file but when output is
written to the file, the current file pointer is
disregarded. All output is written at the end of the file
and causes the file pointer to be repositioned at the end of
the output. If two separate processes open the same file
for append, each process may write freely to the file
without fear of destroying output being written by the
other. The output from the two processes will be intermixed
in the file.
See Also
open(S), fclose(S)
Diagnostics
fopen and freopen return the pointer NULL if filename cannot
be accessed.
Page 2 (printed 8/7/87)