In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces what the Linux system open function is useful, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand it.
The main function of open function in Linux system is to open and create files. We can customize various parameters such as file attributes and user permissions according to the parameters.
First, what is the open function used to do? the open function is generally used to open or create a file under Linux. We can customize the file properties and user permissions and other parameters we need according to the parameters.
Second, the definition and parameters of open function. Let's first 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); 123456 header files as shown above. The header files we need to add when using the open function are
# include// the definition of types pid_t and size_t is provided here. # include # include123 returns the return value of the open function. If the operation is successful, it returns a file descriptor, and if the operation fails, it returns-1.
Parameter meaning: 1, pathname: in the open function, the first parameter pathname 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. The flags: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: readable and writable
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 indicates that if the specified file does not exist, it will be created
O_EXCL indicates that if the file you want to create already exists, an error occurs,-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 writes to the same area to be completed before proceeding.
O_SYNC waits for physical iThano to finish before write, including those that update file attributes.
3. The mode: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 if there is an O_CREAT in the second parameter. If not, the third parameter can be ignored.
Third, the difference between the open function and the fopen function is divided from the source, which 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.
Thank you for reading this article carefully. I hope the article "what is the use of open functions in Linux system" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.