Function open

Top 

Declaration:

int open(char path[], int access);

Description

Opens a file for reading or writing.

open opens the file specified by path and prepares it for reading and/or writing as determined by the value of access. To create a file in a particular mode, you can either assign to the _fmode global variable or call open with the O_CREAT options ORed with the translation mode desired. For example, the call:

open("XMP", O_CREAT | O_BINARY);

creates a binary-mode file named XMP, truncating its length to 0 bytes, if it already exists. For open, access is constructed by performing the bitwise OR with the flags from the following list. Only one flag from the first list can be used (and one must be used); the remaining flags can be used in any logical combination. These symbolic constants are defined in system.h.

Read/Write Flags:

  O_RDONLY     Open for reading only.
  O_WRONLY     Open for writing only.
  O_RDWR       Open for reading and writing

Returned Value

On success, open returns a nonnegative integer (the file handle). The file pointer, which marks the current position in the file, is set to the beginning of the file. On error, open returns -1 and the errno global variable is set to one of the following values:

  EACCES       Permission denied
  EINVACC      Invalid access code
  EMFILE       Too many open files
  ENOENT       No such file or directory