[Bit Notes] Know about files and add some own understanding

file awareness

file name:

A file must have a unique file identifier for user identification and reference. The file name consists of three parts: file path (relative path) or (absolute path) + file name trunk + file suffix.

For example: c:code\test.txt

For convenience, the file ID is often referred to as the file name.

My understanding:

1. The file suffix is ​​after the last . number. The main body of the file name may contain the same string as the file suffix, but it is not the file suffix.

2. Sometimes your system will hide your file suffix. At this time, add it manually. The file suffix may not be the file suffix, but the file trunk. At this time, you have to display your hidden file suffix before you can change it.

file type:

data file:

Depending on how the data is organized, data files are known as text files and binary files.

The data is stored in the form of binary in the internal memory, if it is output to the external storage without adding and changing, it is a binary file .

If it is required to store in the form of ASC11 code on the external storage, it needs to be replaced before storage, and the file stored in the form of ASC11 code is exactly a text file.

How is a piece of data stored in memory?

Characters are all stored in the form of ACSll code, and numeric values ​​can be stored in either ACSll code or binary form.

If there is an integer 10000, and the output is sent to the disk in the form of ACSll code, then the disk will occupy 5 bytes (one byte for each character), while the binary output will only occupy 4 bytes on the disk (vs2022 test ).

fb3cc8a865914ab5816de453cd8d9b8c.png

My understanding:

1. We can understand the text file, but we cannot understand the binary file. It seems to be encrypted.

2. The text file is actually a storage method of our data. With it, there is no need to create new content every time the computer is turned on.

program files:

Including source program file (suffix .c), object file (windows environment suffix .obj), executable program

(Windows environment suffix is ​​.exe).

 

 file buffer

The ANSlC standard adopts the "buffer file system" to process data files. The so-called buffer file system means that the system automatically opens a "file buffer" in the memory for each file in use in the program, and the data output from the memory to the disk will be It is sent to the memory buffer first, and then it is sent to the disk together after the buffer is filled. If the data is read from the disk to the computer, the data is read from the disk file to the memory buffer until the entire buffer is filled, and then the data is sent from the buffer to the program data area (program variables, etc.) one by one. The size of the buffer is determined by the C compilation system.

548cf52b073c4c4cbe9d6856b58c5e91.png

My understanding:

The buffer is like a basket, it will either output when there is no content, or output when it is full. 

file pointer

In the cache file system, the key concept is "file type pointer", referred to as "file pointer".

Each used file has opened a corresponding file information area in the memory, which is used to store the relevant information of the file (such as the name of the file, the modification time of the file, the status of the file, and the current location of the file). This information is stored in a structure variable. The structure type is declared by the system, named "FILE".

36fcbb9f98664e1480dd18049d97f24d.png

 The contents of the FILE type of different compilers are not exactly the same, but they are similar.

Whenever a file is opened, the system will automatically create a variable of the FILE structure according to the situation of the file, and fill in the information, and the user does not need to care about the details.

Generally, the variables of this FILE structure are maintained through a FILE pointer, which is more convenient to use.

Below we can create a FILE* pointer variable:

FILE* pc://file pointer variable

Define pc as a pointer variable pointing to FILE type data, which can make pc point to the file information area of ​​a certain file (it is a structure variable). The file can be accessed through the information in the file information area, that is, the file associated with it can be found through the file pointer.

for example:

 8eed9146c6e642bea4a2f7cf05a936d1.png

 

 file opening and closing

Files should be opened before reading and writing, and should be closed after use.

When writing a program, when opening a file, a FILE* pointer variable will be returned to point to the file, which is equivalent to establishing the relationship between the pointer and the file.

ANSIC stipulates that the fopen function is used to open the file, and the fclose function is used to close the file.

FILE * fopen ( const char*filename, const char*mode);
int fclose(FILE*stream);

 Open as follows:

file usage meaning If the specified file does not exist
"r" (read-only) To enter data, open an existing text file go wrong
"w (write only) To output data, open a text file create a new file
“a” (append) Add data to the end of the text file go wrong
"rb" (read-only) To enter data, open a binary file go wrong
"wb" (write only) To output data, open a binary file create a new file
"ab" (append) append data to the end of a binary file go wrong
"r+" (read and write) Open a text file for reading and writing go wrong
"w+" (read and write) For reading and writing, suggest a new file create a new file
"a+" (read and write) Open a file for reading and writing at the end of the file create a new file
"rb+" (read and write) Open a binary file for reading and writing go wrong

Sequential reading and writing of files

Function Function name apply to
character input function fgetc all input streams
character output function fputc all output streams
text line input function fgets all input streams
text line output function fputs all output streams
format input function fscanf all input streams
format output function fprintf all output streams
binary input fread document
binary output fwrite document

 

Guess you like

Origin blog.csdn.net/2301_77479336/article/details/130039374