Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the meaning of the open parameter in linux

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what is the meaning of open parameters in linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The open function is a common function used to read the linux file IWeiO. The return value of the open function:-1 is returned if the file fails to open, otherwise a value greater than 0 is returned, indicating the file description symbol (which the kernel uses to read the file).

Definition and parameters of open function

Let's first take a look at the definition of open function under Linux.

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

Header file

As shown above, the header file we need to add when using the open function is

# include / / Definitions of types pid_t and size_t are provided here # include # include return value

The return value of the open function returns a file descriptor if the operation is successful and-1 if the operation fails.

Parameter meaning: 1. Pathname:

The first parameter pathname in the open function points to the path name of the file you want to open, or the file name. We need to note that this pathname is an absolute pathname. The file name is under the current path.

2 、 flags:

The flags parameter indicates the operation used to open the file. We need to note that one of the following three constants must be specified, and only one is allowed.

O_RDONLY: read-only mode O_WRONLY: write-only mode O_RDWR: read and write

The following constants are selected, and these options are used to bitwise or combine with the required options above as flags parameters.

O_APPEND means append, and if there is something in the original file, this write will be written at the end of the file. O_CREAT means that if the specified file does not exist, then creating the file O_EXCL means that if the file you want to create already exists, an error occurs and-1 is returned, and the value of errno is modified. O_TRUNC means truncation, and if the file exists and is opened as write-only, read-write, its length is truncated to 0. O_NOCTTY if the pathname points to an end device, do not use this device as a control terminal. O_NONBLOCK if the pathname points to the FIFO/ block file / character file, set the opening of the file and the subsequent Istroke O to non-blocking mode (nonblocking mode)

The following three constants are also selected and are used to synchronize input and output

O_DSYNC waits for the physical Imax O to finish before write. Do not wait for file properties to be updated without affecting the reading of newly written data. O_RSYNC read waits for all write operations to be written to the same area to be completed before O_SYNC, and then write after the completion of physical Iripple O, including I/O3 and mode that update file attributes:

The mode parameter indicates the initial value of setting file access permissions, which is related to the user mask umask. For example, 0644 means-rw-r-r-, can also be defined by S_IRUSR, S_IWUSR and other macros. For more information, please see Man Page of open (2). It should be noted that there are the following points

File permissions are determined by both the mode parameter of open and the umask mask of the current process. The third parameter works only when there is an O_CREAT in the second parameter. If not, the third parameter can ignore the difference between the open function and the fopen function.

In terms of source, it is easy to distinguish between the two:

The open function is a function called by the system under Unix. The file descriptor is returned if the operation is successful, and-1 is returned if the operation fails.

Fopen is a C language library function in the ANSIC standard, so calling API of different kernels on different systems returns a pointer to the file structure.

At the same time, the open function is not buffered, the fopen function is buffered, the open function is generally used with write, and the fopen function is generally used with fwrite.

This is the end of the content of "what is the meaning of the open parameter in linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report