UNIX Advanced Environment Programming Chapter 3 File IO


3.3 Functions open and openat


int open(const char *pathname, int flags);

int open(const char *pathname, int flags, mode_t mode);

pathnameOpen or create file names

flags :

Required options: One of the three constants must be specified, and only one must be allowed.
O_RDONLY: Read-only Open
O_WRONLY: Write Only Open
O_RDWR: Readable Writable Open
O_APPEND denotes additions. If the file already has content, the data written in the open file is appended to the end of the file without overwriting the original content.
O_CREAT Creates this file if it does not exist. When using this option, you need to provide a third parameter, mode, to indicate access to the file.
If O_EXCL specifies O_CREAT at the same time and the file already exists, an error returns.
O_TRUNC truncates a file to 0 bytes if it already exists and opens in a write-only or read-write manner.
For device files, O_NONBLOCK can be opened in O_NONBLOCK mode to do non-blocking I/O (Nonblock I/O).

Mode: Specify file permissions
It can be expressed in octal numbers, such as 0644 for - RW - R - r--, or in bits or in combination with macro definitions such as S_IRUSR and S_IWUSR.
It should be noted that file permissions are determined by both the open mode parameter and the umash mask of the current process.

Recommended Today

[byzer] troubleshooting of Python daemon explosion

background Byzer introduction Byzer is a new generation programming language for data and AI. For specific information, click the following link: https://docs.byzer.org/#/byzer -lang/zh-cn/introduction/byzer_ lang_ design Fix GitHub issues as follows: https://github.com/byzer-org/byzer-lang/issues/1652 Problem description The daemon process is growing rapidly in the background! And the normal situation is onepython envThere should be only one process. Multiple […]