[prev] 40 [next]

File System Operations (cont)

int open(char *Path, int Flags)

int open(char *Path, int Flags, mode_t mode)

  • attempt to open an object at Path, according to Flags
  • flags (defined in <fcntl.h>)
    • O_RDONLY ... open object for reading
    • O_WRONLY ... open object for writing
    • O_APPEND ... append on each write
    • O_RDWR ... open object for reading and writing
    • O_CREAT ... create object if doesn't exist
    • O_TRUNC ... truncate to size 0
  • flags can be combined e.g. (O_WRONLY|O_CREAT)
  • if successful, return file descriptor (small +ve int)
  • if unsuccessful, return -1 and set errno
You should always use the open command that allows you to specify the mode (file permissions) if you are creating a file. Otherwise you will get weird garbage permissions on your newly created files.