libc.a reference

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]



#include <fcntl.h>
#include <sys/stat.h> /* for mode definitions */

int open(const char *file, int mode /*, int permissions */);


This function opens the named file in the given mode, which is any combination of the following bits:


The file is opened for reading.


The file is opened for writing.


The file is opened for both reading and writing.


If the file does not exist, it is created. See section creat.


If the file does exist, it is truncated to zero bytes.


If the file exists, and O_CREAT is also specified, the open call will fail. If the file is a symlink and O_CREAT is also specified, then the contents of the symlink are ignored - the open call will fail.


The file pointer is positioned at the end of the file before each write.


The file is opened in text mode, meaning that Ctrl-M characters are stripped on reading and added on writing as needed. The default mode is specified by the _fmode variable _fmode.


The file is opened in binary mode.

When called to open the console in binary mode, open will disable the generation of SIGINT when you press Ctrl-C (Ctrl-Break will still cause SIGINT), because many programs that use binary reads from the console will also want to get the `^C' characters. You can use the __djgpp_set_ctrl_c library function (see section __djgpp_set_ctrl_c) if you want Ctrl-C to generate interrupts while console is read in binary mode.


Child processes will not inherit this file handle. This is also known as close-on-exec--see fcntl. This bit is DOS- and Windows-specific; portable programs should use fcntl instead.


open will fail with errno set to ELOOP, if the last path component in file is symlink.


If file is a symlink, open will open symlink file itself instead of referred file.


Delete file when all file descriptors that refer to it are closed.

Note that file should not also be opened with the low-level functions _creat, _creatnew, _dos_creat, _dos_creatnew, and _dos_open. Otherwise file may not be deleted as expected.

If the file is created by this call, it will be given the read/write permissions specified by permissions, which may be any combination of these values:


The file is readable. This is always true for MS-DOS.


The file is writable.

Other S_I* values may be included, but they will be ignored.

You can specify the share flags (a DOS specific feature) in mode. And you can indicate default values for the share flags in __djgpp_share_flags. See section __djgpp_share_flags.

You can open directories using open, but there is limited support for POSIX file operations on directories. In particular, directories cannot be read using read (see section read) or written using write (see section write). The principal reason for allowing open to open directories is to support changing directories using fchdir (see section fchdir). If you wish to read the contents of a directory, use the opendir (see section opendir) and readdir (see section readdir) functions instead. File descriptors for directories are not inherited by child programs.

Return Value

If successful, the file descriptor is returned. On error, a negative number is returned and errno is set to indicate the error.


POSIX 1003.2-1992; 1003.1-2001


int q = open("/tmp/foo.dat", O_RDONLY|O_BINARY);

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright © 2004     Updated Apr 2004