System call to manipulate files
1. System calls related to file operations
open()
read()
write()
close()
1)open()
int open(const char* pathname, int flags);// Used to open an existing file
int open(const char* pathname, int flags,mode_t mode);// Used to create a new file and set access permissions
Parameter introduction:
- pathname: the path and name of the file to be opened
- flags: open flag, such as O_WRONLY write only open
- O_RDONLY read only open
- O_RDWR read / write mode on
- O_ Create if the creat file does not exist
- O_APPEND at the end of the file
- O_TRUNC clears the file and writes it again
- mode: permission, such as "0600"
- Return value: file descriptor
2)read()
ssize_t read(int fd, void* buf, size_t count);
Parameter introduction: - fd corresponds to the open file descriptor
- buf space for storing data
- count how many bytes of data do you plan to read from the file at a time
- Return value: the number of bytes actually read
3)write()
ssize_t write(int fd, const void buf,size_t count);*
Parameter introduction: - fd corresponds to the open file descriptor
- buf stores the data to be written
- count plans how much data to write to the file at a time
4)close() - int close(int fd);
- Parameter introduction:
- fd file descriptor to close
2. File descriptor
In Linux system, everything can be regarded as files, which can be divided into ordinary files, directory files, link files and device files. File descriptor is an index created by the kernel to efficiently manage open files. It is a non negative integer (usually a small integer) used to refer to open files. All system calls executing I/O operations pass through the file descriptor. When the program is just started, 0 is the standard input, 1 is the standard output, and 2 is the standard error. If you open a new file at this time, its file descriptor will be 3.
3. Write data in a file
Create a file.txt file in this directory and write hello. The code is as follows:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<unistd.h> 4 #include<string.h> 5 #include<assert.h> 6 #include<fcntl.h> 7 8 int main() 9 { 10 int fd = open("file.txt",O_WRONLY|O_CREAT,0600); 11 assert( fd != -1); 12 13 write(fd,"hello",5); 14 15 close(fd); 16 17 exit(0); 18 19 }
Compile and run, generate the file.txt file, and view the file.txt file
4. Read a file
Read file.txt
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<unistd.h> 4 #include<string.h> 5 #include<assert.h> 6 #include<fcntl.h> 7 8 int main() 9 { 10 11 int fd = open("file.txt",O_RDONLY); 12 assert ( fd != -1 ); 13 14 char buff[128] = {0}; 15 16 int n = read(fd,buff,127); 17 18 printf("n = %d,buff = %s",n,buff); 19 20 close(fd); 21 22 exit(0); 23 24 }
Compile run
5. Write a program to copy an ordinary file (similar to cp command)
Copy the file.txt file in the current path to the current path, named file1.txt, and view the contents of the file.txt file.
Write test.c with the following code:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<unistd.h> 4 #include<string.h> 5 #include<assert.h> 6 #include<fcntl.h> 7 8 int main() 9 { 10 int fdr = open("file.txt",O_RDONLY); 11 int fdw = open("file1.txt",O_WRONLY|O_CREAT,0600); 12 13 if( fdr == -1 || fdw == -1) 14 { 15 printf("File opening failed!"); 16 exit(0); 17 } 18 19 20 char buff[512] = {0}; 21 int num = 0;//How much do you read at a time 22 while((num = read(fdr,buff,512)) > 0) 23 { 24 write(fdw,buff,num); 25 26 } 27 28 close(fdr); 29 close(fdw); 30 31 exit(0); 32 33 }
Compile run
Generate file1.txt file
View the contents of file1.txt file