open a file
int open( const char *path, int oflag, ... );
The open() function opens the file named by path,
creating an open file description that refers to the file, and a file
descriptor that refers to the file description. The file status flags
and the file access modes of the open file description are set
according to the value of oflag.
||The open() function ignores advisory locks that may have
been set by the fcntl(), lock() or locking()
The open file description created is new, and therefore not shared with
any other process in the system.
The value of oflag is constructed by the bitwise OR-ing of values
from the following list, defined in the <fcntl.h>
header file. Applications must specify exactly one of the first three
values (file access modes) below in the value of oflag:
- Open for reading only.
- Open for writing only.
- Open for reading and writing. Opening a FIFO for read/write is
Any combination of the remaining flags may be specified in the value
- If set, the file offset is set to the end of the file prior to
- This option requires a third argument, mode,
which is of type mode_t.
If the file exists, this flag has no effect, except in combination
with O_EXCL as noted below.
Otherwise, the file is created; the file's user ID is set to the
effective user ID of the process; the group ID is set to the effective
group ID. The permission bits, as defined in <sys/stat.h>,
are set to the value of mode, except those bits set in
the process's file mode creation mask (see the function
umask() for details). Bits set in mode other than
permission bits (the file type bits) are ignored. The mode
argument doesn't affect whether the file is opened for reading, for
writing, or for both.
- If O_EXCL and O_CREAT are set,
open() fails if the file exists. The check for the
existence of the file and the creation of the file if it doesn't
exist is atomic with respect to other processes attempting the same
operation with the same filename. Specifying O_EXCL
without O_CREAT has no effect.
- If set, and path identifies a terminal device, the
open() function doesn't cause the terminal device to become
the controlling terminal for the process.
- When opening a FIFO with O_RDONLY or
- If O_NONBLOCK is set:
- Calling open() for reading-only returns
without delay. Calling open() for writing-only
returns an error if no process currently has the FIFO open for
- If O_NONBLOCK is clear:
- Calling open() for reading-only blocks until
a process opens the file for writing. Calling open()
for writing-only blocks until a process opens the file for
- When opening a block special or character special file that
supports nonblocking opens:
- If O_NONBLOCK is set:
- The open() function returns without waiting for
the device to be ready or available. Subsequent behavior of
the device is device-specific.
- If O_NONBLOCK is clear:
- The open() function waits until the device is
ready or available before returning. The definition of when a
device is ready is device-specific.
- Otherwise, the behavior of O_NONBLOCK is
- If the file exists and is a regular file, and the file is successfully
opened O_WRONLY or O_RDWR,
the file length is truncated to zero and the mode and owner are left
unchanged. O_TRUNC has no effect on FIFO or block or
character special files or directories. Using O_TRUNC
with O_RDONLY has no effect.
- If set, this flag affects subsequent I/O calls. If set, each call to
write() waits until all data is successfully transferred
to the storage device such that it is readable on any subsequent open of
the file (even one that follows a system failure) in the absence of a
failure of the physical storage medium. If the physical storage medium
implements a non-writethrough cache, then a system failure may be
interpreted as a failure of the physical storage medium, and data may
not be readable even if this flag is set and the write()
indicates that it succeeded. In practice, this flag has no meaning for
- If set, this flag affects subsequent I/O calls. Each call to
read() or write()
is complete only when both the data has been successfully transferred
(either read or written) and all file system information relevant to
that I/O operation (including that required to retrieve said data) is
successfully transferred, including file update and/or access times,
and so on. See the discussion of a successful data transfer in
- If set, this flag affects subsequent writes. Data written isn't
scheduled to be written to disk (although, depending on the pattern of
system activity, it may be written there). The intention is to keep
the data in cache, if possible, for fast access to temporary files.
In the event of a system failure, no guarantees can be made about the
contents of the file (although it is likely to contain meaningless
data). The data integrity when using O_TEMP with either
O_DSYNC or O_SYNC is indeterminate!
- If set, this flag affects subsequent file reads and writes. This flag
overrides the normal heuristic algorithm limiting file system cache
usage during sequential read and write operations to prevent the cache
from being flushed by large sequential operations. By specifying
O_CACHE, the application is telling the file system to
cache the data regardless of the pattern of usage, in the hopes that it
will be reused again soon. Indiscriminate usage of this flag results
in very poor cache utilization and degraded system performance.
||The open flags O_TEMP and O_CACHE
are QNX extensions, and shouldn't be used in any portable program.
The open flags O_DSYNC and O_SYNC
aren't part of the base POSIX (1003.1), but are from the Real-Time
Extensions (1003.4). As such, they may not exist on all systems
claiming POSIX conformance. They should be used with care in a
If successful, open() returns a nonnegative integer
representing the lowest numbered unused file descriptor. On a file
capable of seeking, the file offset is set to the beginning of the
file. Otherwise, -1 is returned and errno is set to indicate the
If any of the following conditions occur, the open() function
returns -1, and sets errno to the corresponding value.
Refer to the
errno documentation, where a note on
general errors is provided.
- Search permission is denied on a component of the path prefix, or the
file exists and the permissions specified by oflag
are denied, or the file doesn't exist and write permission is denied
for the parent directory of the file to be created.
- While attempting to open the named file, either the file itself or a
component of the path prefix was found to be corrupted. A system
failure - from which no automatic recovery is possible -
occurred while the file was being written to, or while the directory was
being updated. You'll need to invoke appropriate
systems-administration procedures to correct this situation before
- The file named by path is a block special device that is
already open for writing, or path names a file that is on a
file system mounted on a block special device that is already open for
- O_CREAT and O_EXCL are set, and the
named file exists.
- The open() operation was interrupted by a signal.
- The named file is a directory, and the oflag
argument specifies write-only or read/write access.
- Too many file descriptors are currently in use by this process.
- The length of the path string exceeds
PATH_MAX, or a pathname component is longer than
- Too many files are currently open in the system.
- O_CREAT isn't set, and the named file doesn't exist;
or O_CREAT is set and either the path prefix doesn't
exist, or the path argument points to an empty string.
- The directory or file system that would contain the new file cannot
- A component of the path prefix isn't a directory.
- O_NONBLOCK is set, the named file is a FIFO,
O_WRONLY is set, and no process has the file open for
- The named file resides on a read-only file system and either
O_WRONLY, O_RDWR, O_CREAT
(if the file doesn't exist), or O_TRUNC is set in the
/* open a file for output */
/* replace existing file if it exists */
/* with read/write perms for owner */
fd = open( "myfile.dat",
O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR );
/* read a file that is assumed to exist */
fd = open( "myfile.dat", O_RDONLY );
/* append to the end of an existing file */
/* write a new file if file doesn't exist */
/* with full read/write permissions */
fd = open( "myfile.dat",
O_WRONLY | O_CREAT | O_APPEND,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
| S_IROTH | S_IWOTH );
POSIX 1003.1, with POSIX 1003.4 and QNX extensions