System calls to Linux operating files

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:

  1. pathname: the path and name of the file to be opened
  2. flags: open flag, such as O_WRONLY write only open
  3. O_RDONLY read only open
  4. O_RDWR read / write mode on
  5. O_ Create if the creat file does not exist
  6. O_APPEND at the end of the file
  7. O_TRUNC clears the file and writes it again
  8. mode: permission, such as "0600"
  9. Return value: file descriptor
    2)read()
    ssize_t read(int fd, void* buf, size_t count);
    Parameter introduction:
  10. fd corresponds to the open file descriptor
  11. buf space for storing data
  12. count how many bytes of data do you plan to read from the file at a time
  13. Return value: the number of bytes actually read
    3)write()
    ssize_t write(int fd, const void buf,size_t count);*
    Parameter introduction:
  14. fd corresponds to the open file descriptor
  15. buf stores the data to be written
  16. count plans how much data to write to the file at a time
    4)close()
  17. int close(int fd);
  18. Parameter introduction:
  19. 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

Tags: C Linux Unix

Posted on Mon, 01 Nov 2021 11:50:40 -0400 by coldfused