fopen(3S)fopen(3S)NAMEfopen(), freopen(), fdopen() - open/reopen a stream file or convert
file to stream
Opens the file named by
pathname and associates a stream with it. returns a
pointer to the structure associated with the stream.
substitutes the named file in place of the open
stream. The original stream is closed, regardless of
whether the open ultimately succeeds. returns a pointer
to the structure associated with stream and makes an
implicit call to (see ferror(3S)). After a successful
call to the orientation of the stream is cleared (see
is typically used to attach the preopened streams asso‐
ciated with and to other files.
associates a stream with a file descriptor.
File descriptors are obtained from or (see open(2),
dup(2), creat(2), and pipe(2)), which open files but do
not return pointers to a 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 call
are exactly as specified above, except that and 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 in the following values has no effect. It exists to
distinguish binary files from text files; however, there
is no distinction between these types of files on UNIX
systems (it is required for ISO C standard conformance).
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+, or r+b open file for update (reading and
w+, wb+, or w+b truncate file to zero length or cre‐
ate file for update
a+, ab+, or a+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 or to a file positioning function
or and input cannot be directly followed by output without an interven‐
ing call to a file positioning function unless the input operation
When a file is opened for append (that is, when type is or it is impos‐
sible to overwrite information already in the file. All output is
written at the end of the file, regardless of intervening calls to If
two separate processes open the same file for append, each process can
write freely to the file without fear of destroying output being writ‐
ten by the other. Output from the two processes will be intermixed in
the file in the order in which it is written.
HP-UX binary file types are equivalent to their non-binary counter‐
parts. For example, types and are equivalent.
Upon successful completion, and return a pointer to the stream. Other‐
wise, a null pointer is returned and is set to indicate the error.
and fail if:
The fildes argument is not a valid file descriptor.
The type argument is not a valid mode.
There is insufficient space to allocate a buffer.
and fail if:
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.
A signal was caught during
The named file is a directory and
type requires write access.
The calling process has attempted to exceed its open file limit.
The length of the pathname string exceeds or a pathname component is
longer than while is in effect.
The system file table is full.
The named file does not exist or the
pathname argument points to an empty string.
The directory or file system that would contain the new file
cannot be expanded, the file does not exist, and it
was to be created.
A component of the path prefix is not a directory.
The named file is a character special or block special file,
and the device associated with the special file does
The named file is a regular file and the size of the file cannot
be represented correctly in an object of size in this
The named file resides on a read-only file system and
type requires write access.
Additional values can be set by the underlying call made from the and
functions (see open(2)).
SEE ALSOcreat(2), dup(2), open(2), pipe(2), fclose(3S), fgetpos64(3S),
fseek(3S), popen(3S), setbuf(3S), orientation(5), thread_safety(5),