Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
open
◄Summary► ◄Example► ◄Up► ◄Contents► ◄Index► ◄Back►
────────────────────────────────────────────────────────────────────────────
The open function opens the file specified by <filename> and
prepares the file for subsequent reading or writing, as defined
by <oflag>. The <oflag> argument is an integer expression formed
by combining one or more of the following manifest constants
(defined in FCNTL.H):
O_APPEN O_EXCL O_TEXT
O_BINARY O_RDONLY O_TRUNC
O_CREAT O_RDWR O_WRONLY
When more than one manifest constant is given, the constants are
combined with the bitwise-OR operator (|).
See ◄BINMODE.OBJ► for a discussion of binary (O_BINARY) and text
(O_TEXT) modes.
Use the O_TRUNC flag with care, as it destroys the complete
contents of an existing file.
Either O_RDONLY, O_RDWR, or O_WRONLY must be given to specify the
access mode. There is no default value for the access mode.
The <pmode> argument is required only when O_CREAT is specified.
If the file exists, <pmode> is ignored. Otherwise, <pmode>
specifies the file's permission settings, which are set when the
new file is closed for the first time. The <pmode> argument is an
integer expression containing one or both of the manifest
constants S_IWRITE and S_IREAD (defined in SYS\STAT.H). When
both constants are given, they are joined with the bitwise-OR
operator (|).
If write permission is not given, the file is read-only. Under
DOS and OS/2, all files are readable; it is not possible to
give write-only permission. Thus, the modes S_IWRITE and
S_IREAD | S_IWRITE are equivalent.
The open function applies the current file-permission mask to
<pmode> before setting the permissions (see umask).
The <filename> used in the open function is affected by the DOS
APPEND command.
Under DOS versions 3.0 and later with SHARE installed, a
problem occurs when opening a new file with <oflag> set to
O_CREAT | O_RDONLY or O_CREAT | O_WRONLY and <pmode> set to
S_IREAD. In this case, the operating system prematurely closes
the file during system calls made within open.
To get around the problem, open the file with <pmode> set to
S_IWRITE. After closing the file, call chmod and change the mode
back to S_IREAD. Another possibility is to open the file with
<pmode> set to S_IREAD and <oflag> set to O_CREAT | O_RDWR.
Return Value
The open function returns a file handle for the opened file. A
return value of -1 indicates an error, and errno is set to EACCES,
EEXIST, EINVAL, EMFILE, or ENOENT.
-♦-