Recall the C language file operation
- 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:
- 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.
- Example of appending: param2: a
is appended in the form of a, cat to txt with more content.
If mode=w, it will overwrite and delete the previous ones .
-
fputs():
Answer: Write to a specific file. The default three file streams will be opened. -
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) -
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. -
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.
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.
C++ file manipulation
- Write example:
ofstream: std::ios::out has no file, an empty file is generated. Cover if any.
result:
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:
- Write C language files.
- 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.
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.
- Check the log permission after running. Normal
-
The return value of open(): there is a return value. The following 1 outputs 3. 3 is normal, and -1 is failure to open.
-
By the way, where did 0, 1, and 2 go?
Answer: Actually 0: standard input (keyboard), 1: standard output (monitor), 2: standard error (monitor).
-
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.