*Conan The Librarian
Opens a file for reading, writing, or editing. It positions the
file at its beginning (byte 0).
int open (const char *file_spec, int flags, mode_t mode);
int open (const char *file_spec, int flags, . . . ); (DEC C
A null-terminated character string containing a valid file
specification. If you specify a directory in the file_spec and
it is a search list that contains an error, Compaq C interprets
it as a file open error.
The following values are defined in the <file.h> header file:
O_RDONLY Open for reading only
O_WRONLY Open for writing only
O_RDWR Open for reading and writing
O_NDELAY Open for asynchronous input
O_APPEND Append on each write
O_CREAT Create a file if it does not exist
O_TRUNC Create a new version of this file
O_EXCL Error if attempting to create existing file
These flags are set using the bitwise OR operator (|) to
separate specified flags.
Opening a file with O_APPEND causes each write on the file to
be appended to the end. (In contrast, with the VAX C RTL the
behavior of files opened in append mode was to start at EOF and,
thereafter, write at the current file position.)
If O_TRUNC is specified and the file exists, open creates a new
file by incrementing the version number by 1, leaving the old
version in existence.
If O_CREAT is set and the named file does not exist, the Compaq C
RTL creates it with any attributes specified in the fourth and
subsequent arguments ( . . . ). If O_EXCL is set with O_CREAT and
the named file exists, the attempted open returns an error.
An unsigned value that specifies the file-protection mode. The
compiler performs a bitwise AND operation on the mode and the
complement of the current protection mode.
You can construct modes by using the bitwise OR operator (|) to
separate specified modes. The modes are:
The system is given the same access privileges as the owner. A
WRITE privilege also implies a DELETE privilege.
. . .
Optional file attribute arguments. The file attribute arguments
are the same as those used in the creat function. For more
information, see the creat function.
If a version of the file exists, a new file created with open
inherits certain attributes from the existing file unless
those attributes are specified in the open call. The following
attributes are inherited: record format, maximum record size,
carriage control, and file protection.
o If you intend to do random writing to a file, the file
must be opened for update by specifying a flags value of
o To create files with OpenVMS RMS default protections
by using the UNIX system-call functions umask, mkdir,
creat, and open, call mkdir, creat, and open with a
file-protection mode argument of 0777 in a program that
never specifically calls umask. These default protections
include correctly establishing protections based on ACLs,
previous versions of files, and so on.
In programs that do vfork/exec calls, the new process
image inherits whether umask has ever been called or not
from the calling process image. The umask setting and
whether the umask function has ever been called are both
See also creat, read, write, close, dup, dup2, and lseek.
x A nonnegative file descriptor number.
-1 Indicates that the file does not exist, that
it is protected against reading or writing, or
that it cannot be opened for another reason.
flags = O_RDWR; /* Open for read and write, with user */
/* with default file protection, with */
/* max fixed record size of 2048, and */
/* a block size of 2048 bytes. */
file = open("file.dat", flags, 0, "rfm=fix", "mrs=2048", "bls=2048");
if (file == -1)
perror("OPEN error"), exit(1);