fopen, freopen, fdopen
Purpose
Opens a stream.
Library
Standard I/O Library (libc.a)
Syntax
#include <stdio.h>
FILE *fopen (path, type) FILE *fdopen (fildes, type)
char *path, *type; int fildes;
char *type;
FILE *freopen (path, type, stream)
char *path, *type;
FILE *stream;
Description
The fopen subroutine opens the file named by the path
parameter and associates a stream with it. fopen returns
a pointer to the FILE structure of this stream.
The path parameter points to a character string that con-
tains the name of the file to be opened.
The type parameter points to a character string that has
one of the following values:
""r"" Open the file for reading.
""w"" Truncate or create a new file for writing.
""a"" Append (open for writing at end of file, or
create for writing).
""r+"" Open for update (reading and writing).
""w+"" Truncate or create for update.
""a+"" Append (open or create for update at end of
file).
The freopen subroutine substitutes the named file in
place of the open stream. The original stream is closed
whether or not the open succeeds. freopen returns a
pointer to the FILE structure associated with stream.
The freopen subroutine is typically used to attach the
pre-opened streams associated with stdin, stdout, and
stderr to other files.
The fdopen subroutine associates a stream with a file
descriptor obtained from an open, dup, creat, or pipe
system call. These system calls open files but do not
return pointers to FILE structures. Many of the standard
I/O library subroutines require pointers to FILE struc-
tures. Note that the type of stream specified must agree
with the mode of the open file.
When you open a file for update, you can perform both
input and output operations on the resulting stream.
However, an output operation cannot be directly followed
by an input operation without an intervening fseek or
rewind. Also, an input operation cannot be directly fol-
lowed by an output operation without an intervening
fseek, rewind, or an input operation that encounters the
end of the file.
When you open a file for append (that is, when type is
""a"" or ""a+""), it is impossible to overwrite informa-
tion already in the file. You can use fseek to reposi-
tion the file pointer to any position in the file, but
when output is written to the file, the current file
pointer is ignored. All output is written at the end of
the file and causes the file pointer to be repositioned
to the end of the output.
If two separate processes open the same file for append,
each process can write freely to the file without
destroying the output being written by the other. The
output from the two processes is intermixed in the order
in which it is written to the file. Note that if the
data is buffered, then it is not actually written until
it is flushed.
If the fopen or freopen subroutine fails, a NULL pointer
is returned.
Related Information
In this book: "creat," "open," "fclose, fflush," "fseek,
rewind, ftell," "setbuf, setvbuf," and "standard i/o
library."