Open
- You need
- sys/types.h
- And also sys/stat.h
- And probably fcntl.h
- Open: Open a file.
- man 2 open
- This is more complex than it sounds.
- int open(const char *pathname, int flags);
- int open(const char *pathname, int flags, mode_t mode);
- Path is the file name, absolute or relative.
- Flags: A description of the requested file.
- Perms: The permissions used to open the file.
- Return:
- On error: -1
- Otherwise: The lowest available file descriptor, starting at 0.
- This turns out to be important.
- Look at cool1.C
- Mandatory Flag:
- You must use one of the following flags
- On traditional *nix:
- O_RDONLY : read only
- O_WRONLY : write only
- O_RDRW : both reading and writing
- In the past O_RDONLY was 0,
- so O_RDONLY | O_WRONLY = O_WRONLY
- So O_RDRW ≠ O_RDONLY | OWRONLY in older systems.
- This has been deprecated, but ...
- Optional Flags (man 2 open)
- Far too many to discuss here
- Some will can not be used with others.
- Some common flags
- O_CREAT : if the file does not exist, create it.
- This uses the mode argument given later.
- O_TRUNC: if the file exists, truncate it to size 0
- O_APPEND: perform all output at the end of the file.
- O_EXCL: used with O_CREAT, fail if the file exists.
- O_CLOEXEC: close on exec
- Modes
- S_IPUUU
- P = {R,W,X}
- UUU = {USR, GRP, OTH}
- S_IRGRP, S_IWUSR, ...
- Also I_SRWXW
- W = {U,G,O}
- S_IRWXU, S_IRWXG, S_IRWXO
- Only applys with O_CREAT, O_TMPFILE
- Gives future access permission.
- Other elements influence this as well.
- Errors
- There are MANY
- Some depend on the type of file you are trying to open
- Others are related to system limits.
- Or permissions
- Look em over.