Linux programming: common open parameters and linux Parameters
Open is used to open a file. By setting different flags, the process can be read-only, write-only, readable/writable.
For a file (test.txt) that does not exist ), write operation 1/* ========================================== ========================================= 2 * Copyright (C) 2018. all rights reserved. 3*4 * file name: cp. c 5 * Creator: ghostwu (Wu Hua) 6 * creation Date: July 7 * description: use open and write to implement the cp command 8*9 ================================ ========================================= */10 11 # include <stdio. h> 12 # include <sys/types. h> 13 # include <sys/stat. h> 14 # include <fcntl. h> 15 # include <errno. h> 16 17 int main (int argc, char * argv []) 18 {19 int fd, openflags; 20 21 // openflags = O_WRONLY | O_CREAT; 22 23 openflags = O_WRONLY; 24 25 fd = open ("test.txt", openflags); 26 if (fd <0) {27 printf ("% d \ n", errno ); 28 perror ("write to file"); 29} 30 return 0; 31}View Code
1)when test.txt does not exist and the file fails to be opened, an error number will be set for the global variable errno of the operating system. Here 2 is set. For a 2, we have no idea what the error is, therefore, when the perror function is called, it will interpret Number 2 as a readable error message,
It is interpreted as "No such file or directory ",You can use "grep" No such file or directory "/usr/include/*. h" to find the header file with errno 2
22.16if the test.txt file exists, no error will be reported. Open a normal file descriptor and the original file content will not be affected.
2. Add the O_CREAT flag 1/* ================================== ==================================== 2 * Copyright (C) 2018. all rights reserved. 3*4 * file name: cp. c 5 * Creator: ghostwu (Wu Hua) 6 * creation Date: July 7 * description: use open and write to implement the cp command 8*9 ================================ ========================================= */10 11 # include <stdio. h> 12 # include <sys/types. h> 13 # include <sys/stat. h> 14 # include <fcntl. h> 15 # include <errno. h> 16 17 int main (int argc, char * argv []) 18 {19 int fd, openflags; 20 21 // openflags = O_WRONLY | O_CREAT; 22 23 openflags = O_WRONLY | O_CREAT; 24 25 fd = open ("test.txt", openflags); 26 printf ("fd = % d \ n", fd ); 27 if (fd <0) {28 printf ("% d \ n", errno); 29 perror ("write to file"); 30} 31 return 0; 32}View Code
1. If test.txtdoes not exist, test.txt is created and no error is reported.
2XX if test.txtexists, the original content of test.txt will not be affected.
3. Add the O_TRUNC flag
openflags = O_WRONLY | O_CREAT | O_TRUNC;
1. If test.txtdoes not exist, test.txt is created and no error is reported.
2XX if test.txtexists, the original content of test.txt will be cleared.
4. Permission bit 1/* ================================================== ====================================== 2 * Copyright (C) 2018. all rights reserved. 3*4 * file name: cp. c 5 * Creator: ghostwu (Wu Hua) 6 * creation Date: July 7 * description: use open and write to implement the cp command 8*9 ================================ ========================================= */10 11 # include <stdio. h> 12 # include <sys/types. h> 13 # include <sys/stat. h> 14 # include <fcntl. h> 15 # include <errno. h> 16 17 int main (int argc, char * argv []) 18 {19 int fd, openflags; 20 mode_t fileperms; 21 22 openflags = O_WRONLY | O_CREAT | O_TRUNC; 23 24/rwxrw-r-x25 fileperms = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IXOTH; 26 27 fd = open ("test.txt", openflags, fileperms ); 28 printf ("fd = % d \ n", fd); 29 if (fd <0) {30 printf ("% d \ n", errno ); 31 perror ("write to file"); 32} 33 return 0; 34}View Code 5. append O_APPEND