Linux: C/C++ file operation

Recall the C language file operation

  1. Use of fopen():
      Answer: Open the file stream pointer.
    param2 = "w", if there is no file currently, it will be created first, and then written.
    "r", read-only.
    "b", can be combined with w, b.
  • Write-only example:
    Please add a picture description
    Please add a picture description
  • Read-only example:
    rely on fgets(), but also use buffer, and finally judge whether there is a reading error in the file. If there is a reading error, fgets is also 0.
    Please add a picture description
    Please add a picture description
  • Example of appending: param2: a
    Please add a picture description
    is appended in the form of a, cat to txt with more content.
    Please add a picture description
    If mode=w, it will overwrite and delete the previous ones .
  1. fputs():
      Answer: Write to a specific file. The default three file streams will be opened.

  2. fgets():
      Answer: Only for reading, you need to use the buffer and give the size on the second parameter.
    For the example of reading files in 1, give fgets(buffer, buffer_size, file_p)

  3. C language file operation fputs default open output stream:
      Answer: fputs() will open stdin, stdout, stderr . The essence of these three streams is FILE*.
    Corresponding equipment:
    stdin: keyboard
    stdout: monitor
    stderr: monitor
    and C++ will open: cin, cout, cerr.

  4. Experiment: The test stream stdout corresponds to the monitor
    Observe the following code, if fputs opens three file streams by default, then the device monitor corresponding to stdout should be opened. So writing msg to stdout will be output directly on the display.
    Please add a picture description
    Please add a picture description
    So far, it has been verified to write directly to the display through stdout.
    In short, although the programming language may interact with the OS, drivers, etc., the essence is actually to operate on the hardware, so the "file" operations in the language, such as functions: fopen, fclose, fread, fwritefputs, fgets, etc., must run through the operating system.
    Please add a picture description

C++ file manipulation

  • Write example:
    ofstream: std::ios::out has no file, an empty file is generated. Cover if any.
    Please add a picture description
      result:
    Please add a picture description

WRONLY: Open for writing, O_CREAT: Create if the file does not exist. In addition, 0644 is a permission parameter, if not added, it will cause permission confusion as follows:
Please add a picture description

  1. Write C language files.
    Please add a picture description
  • About open():
    param2: flags is the flag bit. There are 32 bits, as in the second line above, there are two flag bits. You can use | to pass multiple. After you pass in the parameters, the bottom layer will do the & operation. The O_WRONLY and O_CREAT above are all data with only one bit being 1, and they are not repeated.
    Each value converted to binary has only one bit that is 1. So two values ​​or one, let the two positions be 11, so do & with the incoming parameter, you can get the real desired value.
  • A mug command to see the true size of these flag values ​​in system source files:
grep -ER 'O_CREATE|O_RDONLY' /usr/include/

grep -E is filter search, R is recursive search.
Please add a picture description
Next, go to /usr/include/asm-generic/fcntl.h as shown in the picture above
and edit this file through vim, and then check on the command line: ./RDONLY and found that it is all 0 and all 1.
Please add a picture description

  1. Check the log permission after running. NormalPlease add a picture description
  • The return value of open(): there is a return value. The following 1 outputs 3. 3 is normal, and -1 is failure to open.
    Please add a picture description
    Please add a picture description

  • By the way, where did 0, 1, and 2 go?
      Answer: Actually 0: standard input (keyboard), 1: standard output (monitor), 2: standard error (monitor).
    Please add a picture description

  • The nature of the system opening files:
      Answer: The essence of all file operations is that the process executes the corresponding function, the process operates on the file, and the file must be opened first. Load file-related attribute information into memory . A process can open multiple files.

  • The process will open many files, the operating system must manage the open files, how to manage it?
      A: Describe first, then organize . There is a data structure here: file, which protects the related attributes and link attributes of the opened file.

Guess you like

Origin blog.csdn.net/myscratch/article/details/127192223