[Previous] [Contents] [Index] [Next]


open(), open64()

Open a file


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int open( const char * path,
          int oflag, 
          ... );

int open64( const char * path, 
            int oflag, 
            ... );


The path name of the file that you want to open.
Flags that specify the status and access modes of the file; see below.

If you set O_CREAT in oflag, you must also specify the following argument:

An object of type mode_t that specifies the access mode that you want to use for a newly created file. For more information, see "Access permissions" in the documentation for stat(), and the description of O_CREAT, below.





The open() and open64() functions open 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.

Note: These functions ignore any advisory locks that you set with fcntl().

The open file descriptor created is new, and therefore isn't shared with any other process in the system.

Construct the value of oflag by bitwise ORing values from the following list, defined in the <fcntl.h> header file. You must specify exactly one of the first three values (file access modes) below in the value of oflag:

Open for reading only.
Open for reading and writing. Opening a FIFO for read-write is unsupported.
Open for writing only.

You can also specify any combination of the remaining flags in the value of oflag:

Close the file descriptor on execution.
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 of the process or the group ID of the file's parent directory (see chmod()).

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 umask() for details). Bits set in mode other than the file permission bits (i.e. the file type bits) are ignored. The mode argument doesn't affect whether the file is opened for reading, for writing, or for both.

Allow the file offset to be 64 bits long.
Use the real uid/gid for permissions checking.

The largest value that can be represented correctly in an object of type off_t shall be established as the offset maximum in the open file description.


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 (errno is set).

Note: In QNX Neutrino, the returned file descriptor is the same as the connection ID (or coid) used by the Neutrino-specific functions.


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 proceeding.
File access was denied due to a conflicting open (see sopen()).
The O_CREAT and O_EXCL flags are set, and the named file exists.
The open() operation was interrupted by a signal.
The requested synchronized modes (O_SYNC, O_DSYNC, O_RSYNC) aren't supported.
The named file is a directory, and the oflag argument specifies write-only or read/write access.
Too many levels of symbolic links or prefixes.
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 NAME_MAX.
Too many files are currently open in the system.
The O_CREAT flag 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 filesystem that would contain the new file can't be extended.
The open() function isn't implemented for the filesystem specified in path.
A component of the path prefix isn't a directory.
The O_NONBLOCK flag is set, the named file is a FIFO, O_WRONLY is set, no process has the file open for reading, or the media associated with the file has been removed (e.g. CD, floppy).
The named file is a regular file and the size of the file can't be represented correctly in an object of type off_t.
The named file resides on a read-only filesystem and either O_WRONLY, O_RDWR, O_CREAT (if the file doesn't exist), or O_TRUNC is set in the oflag argument.


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

int main( void )
    int fd;

    /* open a file for output              */
    /* replace existing file if it exists  */
    /* with read/write perms for owner     */

    fd = open( "myfile.dat",
        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",
        | S_IROTH | S_IWOTH );
    return EXIT_SUCCESS;


open() is POSIX 1003.1; open64() is Large-file support

Cancellation point Yes
Interrupt handler No
Signal handler Yes
Thread Yes


The open() function includes POSIX 1003.1-1996 and QNX extensions.

See also:

chmod(), close(), creat(), dup(), dup2(), errno, fcntl(), fstat(), lseek(), read(), write()

[Previous] [Contents] [Index] [Next]