FOPEN(3S)
NAME
fopen, freopen, fdopen − open or re-open a stream file; convert file to stream
SYNOPSIS
#include <stdio.h>
FILE ∗fopen (file_name, type)
const char ∗file_name, ∗type;
FILE ∗freopen (file_name, type, stream)
const char ∗file_name, ∗type;
FILE ∗stream;
FILE ∗fdopen (filedes, type)
int filedes;
const char ∗type;
DESCRIPTION
Fopen opens the file named by file_name and associates a stream with it. Fopen returns a pointer to the FILE structure associated with the stream.
File_name points to a character string that contains the name of the file to be opened.
Type is a character string having one of the following values:
"r" open for reading
"w" truncate to zero length or create for writing
"a" append; open for writing at end of file, or create for writing
"rb" open binary file for reading
"wb" truncate to zero length or create binary file for writing
"ab" append; open binary file for writing at end-of-file, or create binary file
"r+" open for update (reading and writing)
"w+" truncate to zero length or create for update
"a+" append; open or create for update at end-of-file
"r+b" or "rb+"
open binary file for update (reading and writing)
"w+b" or "wb+"
truncate to zero length or create binary file for update
"a+b" or "ab+"
append; open or create binary file for update at end-of-file
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)).
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(2), dup(2), creat(2), or 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.
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 call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input may not 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" or "a+"), it is impossible to overwrite information already in the file. All output is written at the end of the file, regardless of intervening class to the fseek function. 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. The output from the two processes will be intermixed in the file in the order in which it is written.
DIAGNOSTICS
Fopen and freopen return a NULL pointer if file_name cannot be accessed, if there are too many open files, or if the arguments are incorrect.
Fdopen returns a NULL upon failure.
NOTES
On HP-UX the binary file types are equivalent to their non-binary counterparts. For example, the "r" and "rb" types are equivalent.
SEE ALSO
creat(2), dup(2), open(2), pipe(2), fclose(3S), fseek(3S), popen(3S).
STANDARDS CONFORMANCE
fopen: SVID2, XPG2, XPG3, POSIX.1, FIPS 151-1, ANSI C
fdopen: SVID2, XPG2, XPG3, POSIX.1, FIPS 151-1
freopen: SVID2, XPG2, XPG3, POSIX.1, FIPS 151-1, ANSI C
Hewlett-Packard Company — HP-UX Release 7.0: Sept 1989