fopen(3) — Subroutines
OSF
NAME
fopen, freopen, fdopen - Opens a stream
LIBRARY
Standard C Library (libc.a)
SYNOPSIS
#include <stdio.h>
FILE ∗fopen (
const char ∗path,
const char ∗type );
FILE ∗fdopen (
int filedes,
const char ∗type );
FILE ∗freopen (
const char ∗path,
const char ∗type,
FILE ∗stream );
PARAMETERS
pathPoints to a character string that contains the name of the file to be opened. If the final component of the path parameter specifies a symbolic link, the link is traversed and pathname resolution continues.
typePoints to a character string that has one of the following values:
rOpen text file for reading.
wCreate a new text file for writing, or open and truncate to zero length. (The file is not truncated under the fdopen() function.)
aAppend (open text file for writing at the end of the file, or create for writing).
rbOpen binary file for reading.
wbCreate a binary file for writing, or open and truncate to zero length.
abAppend (open binary file for update, writing at the end of the file, or create for writing).
r+Open for update (reading and writing).
w+Truncate or create for update. (The file is not truncated under the fdopen() function.)
a+Append (open text file for update, writing at End-of-File, or create for writing).
r+b or rb+Open binary file for update (reading and writing).
w+b or wb+
Create binary file for update, or open and truncate to zero length.
a+b or ab+Append (open a binary file for update, writing at the end of the file, or create for writing).
OSF/1 does not distinguish between text and binary files.
streamSpecifies the input stream.
filedesSpecifies a valid open file descriptor.
DESCRIPTION
The fopen() function opens the file named by the path parameter and associates a stream with it, returning a pointer to the FILE structure of this stream.
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 fflush() function call or a file positioning operation (fseek(), fsetpos(), or rewind function). Also, an input operation cannot be directly followed by an output operation without an intervening flush or file positioning operation, unless the input operation encounters the end of the file.
When you open a file for append (that is, when the type parameter is a or a+), it is impossible to overwrite information already in the file. You can use the fseek() function to reposition 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 the file pointer is 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, it is not actually written until it is flushed.
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 for the stream are cleared.
If the type parameter is w, a, w+, or a+ and the file did not previously exist, upon successful completion the fopen() function marks the st_atime, st_ctime and st_mtime fields of the file and the st_ctime and st_mtime fields of the parent directory for update. If the type parameter is w or w+ and the file did previously exist, upon successful completion the fopen() function marks the st_ctime and st_mtime fields of the file for update.
The freopen() function substitutes the named file in place of the open stream. The original stream is closed regardless of whether the open() function succeeds with the named file. The freopen() function returns a pointer to the FILE structure associated with the stream parameter. The freopen() function is typically used to attach the preopened streams associated with stdin, stdout, and stderr to other files.
The fdopen() function associates a stream with a file descriptor obtained from an open(), dup(), creat(), or pipe() function. These functions open files but do not return pointers to FILE structures. Many of the standard I/O package functions require pointers to FILE structures. Note that the type of stream specified must agree with the mode of the open file.
NOTES
AES Support Level:
Full use
RETURN VALUES
If the fopen(), fdopen(), or freopen() function fails, a null pointer is returned and errno may be set to indicate the error.
ERRORS
If the fopen() function fails, errno may be set to one of the following values:
[EACCES]Search permission is denied on a component of the path prefix, or the file exists and the permissions specified by the type parameter are denied, or the file does not exist and write permission is denied for the parent directory of the file to be created.
[EINTR]The fopen() function was interrupted by a signal which was caught.
[EISDIR]The named file is a directory and type requires write access.
[EMFILE]OPEN_MAX file descriptors are currently open in the calling process.
[ELOOP]Too many links were encountered in translating path.
[ENAMETOOLONG]
The length of the path string exceeds PATH_MAX or a pathname component is longer than NAME_MAX.
[ENFILE]Too many files are currently open in the system.
[ENOENT]The named file does not exist or the path parameter points to an empty string.
[ENOSPC]The directory or file system that would contain the new file cannot be expanded.
[ENOTDIR]A component of the path prefix is not a directory.
[ENXIO]The named file is a character special or block special file and the device associated with this special file does not exist.
[EROFS]The named file resides on a read only file system and type requires write access.
[ETXTBSY]The file is being executed and mode requires write access.
If the fdopen() function fails, errno may be set to one of the following values:
[EBADF]The filedes parameter is not a valid file descriptor.
[EINVAL]The type parameter is not a valid mode.
[ENOMEM]Insufficient space to allocate a buffer.
The freopen() function fails if the following is true:
[EACCES]Search permission is denied on a component of the path prefix, or the file exists and the permissions specified by the type parameter are denied, or the file does not exist and write permission is denied for the parent directory of the file to be created.
[EINTR]The freopen() function was interrupted by a signal which was caught.
[EISDIR]The named file is a directory and type requires write access.
[EMFILE]OPEN_MAX file descriptors are currently open in the calling process.
[ELOOP]Too many links were encountered in translating path.
[ENAMETOOLONG]
The length of the path string exceeds PATH_MAX or a pathname component is longer than NAME_MAX.
[ENFILE]Too many files are currently open in the system.
[ENOENT]The named file does not exist or the path parameter points to an empty string.
[ENOSPC]The directory or file system that would contain the new file cannot be expanded.
[ENOTDIR]A component of the path prefix is not a directory.
[ENXIO]The named file is a character special or block special file, and the device associated with this special file does not exist.
[EROFS]The named file resides on a read only file system and type requires write access.
[EINVAL]The type parameter is not a valid type.
[ETXTBSY]The file is being executed and mode requires write access.