In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use the Linux open command". In the daily operation, I believe many people have doubts about how to use the Linux open command. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about how to use the Linux open command. Next, please follow the editor to study!
Open command is used to open the file, use the need to reference the header file, if failed, return-1, otherwise return a positive integer, 0 is the standard input stream, 1 is the standard output stream, 2 is the standard error stream, other files are incremented from 3. The function is defined as follows
_ _ extern_always_inline int open (_ _ const char * _ _ path, int _ _ oflag,...) {if (_ _ va_arg_pack_len () > 1) _ _ open_too_many_args () If (_ _ builtin_constant_p (_ _ oflag)) {if ((_ _ oflag & O_CREAT)! = 0 & & _ va_arg_pack_len () input parameter. Path is the path of the file to be opened or created, and oflag is the tag, which is used to identify the permissions of the file to be opened or created. It is composed of the primary flag and the standby flag. The primary flag has O_RDONLY, O_WRONLY and O_RDWR, respectively, indicating read-only and write-only. Read and write, there must be one of the three, standby flag is free to choose, a commonly used backup flag is O_CREAT, which is used to create the file under the _ _ path path when the file is not available. You can add a third parameter, whose type is mode_t, which is an octal number. Three digits are used to represent owner permissions, group permissions, and other user permissions. Each parameter consists of 4 | 2 | 1, 4 is read, 2 is write, and 1 is execution. For example, 0600 means the owner has read and write permissions (6 = 4 | 2). An example code: # ifdef _ cplusplus extern "C" {# endif # include # include # include / / for open # include / / for printf void main (argc, argv) int argc; char * * argv; {char * filename = "lala"; / / the file to be created char * str = "hello, world\ n"; / / the content to be written to the file int fd1 = open (filename, O_RDWR | O_CREAT, 0644) Printf ("tmp =% d\ n", fd1); write (fd1, str, strlen (str) + 1); system ("cat lala"); int fd2 = open (filename, O_CREAT | O_WRONLY | O_EXCL, 0600); if (- 1 = = fd2) {fd2 = open (filename, O_WRONLY | O_EXCL | O_TRUNC, 0600); printf ("fd =% d\ n", fd2); / / fd2 will be 1} close (fd1) higher than fd1 Close (fd2); system ("cat lala");} # ifdef _ cplusplus} # endif this ends the study of "how to use the Linux open command", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.