linux system programming: detailed explanation of open common parameters

Posted by Boris Senker on Fri, 01 May 2020 22:34:42 +0200

Open is used to open a file. By setting different flag s, you can make the process read-only, write only, read / write and other operations

1, Write to a non-existent or existing file (test.txt)

 1 /*================================================================
 2 *   Copyright (C) 2018 . All rights reserved.
 3 *   
 4 *   File name: cp.c
 5 *   Creator: ghostwu (Wu Hua)
 6 *   Date of establishment: January 10, 2018
 7 *   Description: using open and write to implement 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 }

1) When test.txt does not exist, the file fails to be opened, and an error number will be set for the global variable errno of the operating system. Here it is set as 2. For a 2, we do not know what the error is at all, so when calling the perror function, it will interpret the number 2 as a readable error message,

This is interpreted as "No such file or directory". You can find the header file with errno as 2 through "grep" No such file or directory "/ usr / include / * / *. H"

2) If the test.txt file exists, no error will be reported. Open a normal file descriptor, and the original content of the file will not be affected

2, Add o'creat flag

 1 /*================================================================
 2 *   Copyright (C) 2018 . All rights reserved.
 3 *   
 4 *   File name: cp.c
 5 *   Creator: ghostwu (Wu Hua)
 6 *   Date of establishment: January 10, 2018
 7 *   Description: using open and write to implement 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 }

1) If test.txt does not exist, create test.txt without error

2) If test.txt exists, 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.txt does not exist, create test.txt without error

2) If test.txt exists, 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 *   Date of establishment: January 10, 2018
 7 *   Description: using open and write to implement 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-x
25     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 }

V. o'append

Topics: Linux