Linux basic IO (1): file operating system interface

(1) open interface

image-20221102210112928

【Parameter Description】:

  • pathname: Path to open file (relative/absolute)

  • flags: file status flag macro

    > (1) Design strategy

    ​ If a file has multiple states, multiple file marks need to be passed in, but we only need one bit to indicate whether a certain state exists, so Linux usesbitmapThe idea to describe the state of the file.

    ​ Each flag macro generally only needs to have one bit to be 1, and it corresponds to other macros and cannot overlap. Let's use the following example to deepen our understanding.

    ​ Through bit operations |, we can realize the combination of multiple states.

    image-20221102214400931

    > Common macros
    1. Must contain O_RDONLY , O_WRONLYor O_RDWR, respectively represent read-only, write-only, readable and writable
    2. Can be combined |with
      • O_APPEND: The file pointer is positioned to the end
      • O_CREAT: Create a file when the file does not exist
      • O_TRUNC: truncate the file length to 0, that is, clear the original content of the file
    > Compared with the C language opening method
    1. w:O_WRONLY | O_CREAT | O_TRUNC
    2. a:O_WRONLY | O_CREAT | O_APPEND
    3. r:O_RDONLY
  • mode: Specifies the access permissions of the file.

    > Do not specify access rights

    ​ When you need to create a new file, it is necessary to specify the mode, otherwise the permissions of the created file will be wrong and confusing.

    image-20221102220440523

    > Specify Access Rights

    image-20221102220754166

    image-20221102220814534

    ​ Although we specified the file permission as 0666, we found that the file permission of the actually generated log.txt is 0664, because it is affected by the mask (the default mask is 0002)

    ​ ​ If we want to generate a file with permissions of 0666, we can use the system interface umaskto reset the mask (note that umask only affects files that have not been created, and if the current file exists, using umaks will not work)

    image-20221102222219253

    image-20221102222319652

  • Return value: corresponding to the opened filefile descriptor

(2) close interface

image-20221102224631051

【Parameter Description】

  • fd: file descriptor

(3) write interface

image-20221102224800282

【Parameter Description】:

  • fd: file descriptor
  • buf: write data from the buf buffer to the file (can be any type of data)
  • count: the number of bytes written

【Use Cases】:

image-20221103141516702

【Notice】:

​ Note that there is no need to add '\0' at the end when writing. This is the sign of the end of the C language string, but this set is not recognized in the file, otherwise you will see the following things later.

image-20221103141621995

(4) read() interface

image-20221102225108035

【Parameter Description】:

  • fd: file descriptor
  • buf: read data from the buf buffer to the file (can be any type of data)
  • count: the number of bytes read
  • Return value: Returns the number of bits actually read if successful, returns -1 if failed.

【Use Cases】:

image-20221103142744456

Note that one bit should be reserved for '\0' when reading .

Guess you like

Origin blog.csdn.net/whc18858/article/details/127704561