fopen(3S) fopen(3S)
NAME
fopen, freopen, fdopen, fopen64, freopen64 - open a stream
SYNOPSIS
#include <stdio.h>
FILE *fopen(const char *filename, const char *type);
FILE *freopen(const char *filename, const char *type, FILE *stream);
FILE *fdopen(int fildes, const char *type);
FILE *fopen64(const char *filename, const char *mode);
FILE *freopen64(const char *filename, const char *mode, FILE *stream);
DESCRIPTION
fopen() opens the file named by filename and returns a pointer to the
FILE structure associated with the filename. filename must point to a
string containing the name of the file to be opened. The type of
access required is specified in the type string, which can have one of
the following values.
r or rb Open file for reading
w or wb Truncate file to zero length for writing, or
create
a or ab Append; open for writing at end of file, or create
for writing
r+, r+b or rb+ Open file for update (reading and writing)
w+, w+b or wb+ Truncate or create for update
a+, a+b or ab+ Append; open or create for update at end-of-file
The b is ignored in the above types. It exists to distinguish binary
files from text files. However, there is no distinction between these
types of files on a Reliant UNIX system.
freopen() substitutes the opened file designated by stream with the
file whose pathname is pointed to by filename. A flush is first
attempted, and then the original stream is closed, regardless of
whether the open ultimately succeeds. Failure to flush or close stream
successfully is ignored. freopen() returns a pointer to the FILE
structure associated with stream.
freopen() is typically used to attach the preopened streams associated
with stdin, stdout, and stderr to other files. stderr is by default
unbuffered, but the use of freopen() will cause it to become buffered
or line-buffered.
Page 1 Reliant UNIX 5.44 Printed 11/98
fopen(3S) fopen(3S)
fdopen() associates a stream with a file descriptor. File descriptors
are obtained from open(), dup(), creat(), or pipe(), which open files
but do not return pointers to a FILE structure stream. Streams are
necessary input for almost all of the Section 3S library routines. The
stream type must agree with the mode of the open file. The file posi-
tion indicator associated with the stream is set to the position indi-
cated by the file offset associated with fildes.
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 fflush(), fseek(), fsetpos(), or
rewind(), and input may not be directly followed by output without an
intervening fseek(), fsetpos(), or rewind(), or an input operation
that encounters end-of-file.
When a file is opened for append (i.e., when type is a, ab, a+, or
ab+), 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 out-
put. 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 in the order in which it is written.
When opened, a stream is fully buffered if and only if it can be
determined not to refer to an interactive device. The error and end-
of-file indicators are cleared for the stream when it is opened.
The largest value that can be represented correctly in an object of
type offt will be established as the offset maximum in the open file
description.
There is no functional difference between fopen()/freopen() and
fopen64()/freopen64(), except for the interpretation of off64t [see
lfs(5)].
ERRORS
The following error code descriptions are function-specific. You will
find a general description in introprm2(2) or in errno(5).
fopen() will fail if:
EACCES Search permission is denied on a component of the path
prefix, or the file exists and the permissions specified
by type are denied, or the file does not exist and write
permission is denied for the parent directory of the
file to be created.
EINTR A signal was caught during the fopen() function.
Page 2 Reliant UNIX 5.44 Printed 11/98
fopen(3S) fopen(3S)
EISDIR The named file is a directory and type requires write
access.
ELOOP Too many symbolic links were encountered in resolving
path.
EMFILE OPENMAX file descriptors are currently open in the cal-
ling process.
ENAMETOOLONG The length of the filename exceeds PATHMAX or a path-
name component is longer than NAMEMAX.
ENFILE The maximum allowable number of files is currently open
on the system.
ENOENT The named file does not exist or the filename argument
points to an empty string.
ENOSPC The file does not exist and the directory that would
contain the new file cannot be expanded.
ENOTDIR A component of the path prefix is not a directory.
ENXIO The file is a special file and cannot be accessed.
EOVERFLOW The named file is a regular file and the size of the
file cannot be represented correctly in an object of
type offt.
EROFS The file resides on a read-only file system, and type
requires write access.
The fopen() function may fail if:
EINVAL The value of the type argument is not valid.
EMFILE STREAMMAX streams are currently open in the calling
process.
ENAMETOOLONG Pathname resolution of a symbolic link produced an
intermediate result whose length exceeds PATHMAX.
ENOMEM Insufficient storage space is available.
ETXTBSY The file is a pure procedure (shared text) file that is
being executed and type requires write access.
The fdopen() function may fail if:
EBADF The fildes argument is not a valid file descriptor.
Page 3 Reliant UNIX 5.44 Printed 11/98
fopen(3S) fopen(3S)
The freopen() function will fail if:
ENXIO The named file is a character special or block special
file, and the device associated with this special file
does not exist.
EOVERFLOW The named file is a regular file and the size of the
file cannot be represented correctly in an object of
type offt.
RESULT
The functions fopen() and freopen() return a null pointer if filename
cannot be accessed, or if type is invalid, or if the file cannot be
opened.
The function fdopen() returns a null pointer if fildes is not an open
file descriptor, or if type is invalid, or if the file cannot be
opened.
The functions fopen() or fdopen() will fail and not set errno if there
are no free stdio(3S) streams.
File descriptors used by fdopen() must be less than 255.
NOTES
STREAMMAX is the number of streams that one process can have open at
one time. If defined, it has the same value as FOPENMAX.
SEE ALSO
close(2), creat(2), dup(2), open(2), pipe(2), write(2), fclose(3S),
fseek(3S), setbuf(3S), stdio(3S), lfs(5), stdio(5).
Page 4 Reliant UNIX 5.44 Printed 11/98