HP-UX Reference (11i v2 03/08) - 3 Library Functions A-M (vol 6)
f
fopen(3S) fopen(3S)
NAME
fopen( ), freopen( ), fdopen( ) - open or re-open a stream file; convert file to stream
SYNOPSIS
#include <stdio.h>
FILE *fopen(const char *pathname, const char *type);
FILE *freopen(const char *pathname, const char *type, FILE *stream);
FILE *fdopen(int fildes, const char *type);
DESCRIPTION
fopen() Opens the file named by pathname and associates a stream with it.
fopen() returns a
pointer to the
FILE structure associated with the stream.
freopen() substitutes the named file in place of the open stream. The original stream is closed,
regardless of whether the open ultimately succeeds.
freopen()
returns a pointer to
the
FILE structure associated with stream and makes an implicit call to
clearerr()
(see ferror(3S)). After a successful call to
freopen(), the orientation of the stream is
cleared (see orientation (5)).
freopen() is typically used to attach the preopened streams associated with stdin,
stdout, and stderr to other files.
fdopen() associates a stream with a file descriptor. File descriptors are obtained from open(),
dup(), creat(),or pipe() (see open(2), dup(2), creat (2), and pipe(2)), which open
files but do not return pointers to a FILE structure stream. Streams are necessary
input for many of the Section (3S) library routines. The type of stream must agree with
the mode of the open file. The meanings of type used in the fdopen() call are exactly
as specified above, except that w, w+, wb, and wb+ do not cause truncation of the file.
pathname Points to a character string containing the name of the file to be opened.
type Character string having one of the values listed below. The
b in the following values has
no effect. It exists to distinguish binary files from text files; however, there is no distinc-
tion between these types of files on UNIX systems (it is required for ISO C standard con-
formance).
r or rb open file for reading
w or wb truncate to zero length or create file for writing
a or ab append; open file for writing at end of file, or create file or writing
r+, rb+,orr+b open file for update (reading and writing)
w+, wb+,orw+b truncate file to zero length or create file for update
a+, ab+,ora+b append; open or create file for update at end-of-file
When a file is opened for update, both input and output can be done on the resulting stream . However,
output cannot be directly followed by input without an intervening call to
fflush() or to a file position-
ing function (fseek(), fsetpos(),orrewind()), and input cannot be directly followed by output
without an intervening call to a file positioning function unless the input operation encounters end-of-file.
When a file is opened for append (i.e., when type is
a, a+, ab+,ora+b), it is impossible to overwrite
information already in the file. All output is written at the end of the file, regardless of intervening calls
to fseek(). If two separate processes open the same file for append, each process can write freely to the
file without fear of destroying output being written by the other. Output from the two processes will be
intermixed in the file in the order in which it is written.
Notes
HP-UX binary file types are equivalent to their non-binary counterparts. For example, types
r and rb
are equivalent.
RETURN VALUE
Upon successful completion,
fopen(), fdopen() and freopen() return a FILE * pointer to the
stream. Otherwise, a null pointer is returned and errno is set to indicate the error.
HP-UX 11i Version 2: August 2003 − 1 − Hewlett-Packard Company Section 3−−317