VMS Help
CRTL, open
*Conan The Librarian
|
Opens a file for reading, writing, or editing. It positions the
file at its beginning (byte 0).
Format
#include <fcntl.h>
int open (const char *file_spec, int flags, mode_t mode);
(ANSI C)
int open (const char *file_spec, int flags, . . . );
(DEC C Extension)
file_spec
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.
If the file_spec parameter refers to a symbolic link, the open
function opens the file pointed to by the symbolic link.
flags
The following values are defined in the <fcntl.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.
mode
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:
0400 OWNER:READ
0200 OWNER:WRITE
0100 OWNER:EXECUTE
0040 GROUP:READ
0020 GROUP:WRITE
0010 GROUP:EXECUTE
0004 WORLD:READ
0002 WORLD:WRITE
0001 WORLD:EXECUTE
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.
NOTES
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_RDWR.
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
inherited attributes.
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.
#include <unixio.h>
#include <fcntl.h>
#include <stdlib.h>
main()
{
int file,
stat;
int flags;
flags = O_
RDWR; /* Open for read and write, */
/* with user 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);
close(file);
}