C language File manipulation Corpus

  • 2020-06-03 07:48:22
  • OfStack

1. Files are divided into ASCII files and base 2 files. ASCII files, also known as text files, are composed of 1 series of characters.

2.

FILE is a file type identifier and a structure type defined by the C compilation system. The structure contains file name, file state and other information.
It defines a pointer variable, fp, used to point to a file that holds the first address of the file buffer.

3. File opening: fopen ();

There are 12 ways to access files in C language. read, write, append (append), text (text file), banary (base 2 file), + means read and write.

4. File closure: fclose ();

The operating system has a limit on the number of files that can be opened. When too many files are opened, the reading and writing between files will affect each other, so it is important to close files that are not in use.
This function normally closes the file returns 0, otherwise -1;

When the file is closed, the system cleans up the buffer pointed to by fp, outputs the data to the disk file, and then releases the buffer unit, decoupling the file pointer from the specific file. This prevents files from being lost and information from being destroyed.

However, when using C language to operate files, sometimes messy code will occur. At this time, you should check 1 of your own procedures, whether the variables written to the file are initialized, and whether the file is closed properly. When variables are not initialized, garbled code is often referred to as "scalding."

There is also a possibility that the file encoding method and the program encoding method is not 1. TXT files under Windows are encoded by ANSI. Be aware of how your compiler encodes.

r stands for short for read, + for readable and writable, w for write, b for bit2 and t for text

r opens a read-only file, which must exist
r+ opens a readable and writable file that must exist (in this case, the write file overwrites the previous file)
rt opens a read-only text file, which must exist
rt+ Read and write opens 1 text file, allowing read and write, which must exist (in this case, the write file overwrites the previous file)
rb read-only opens a binary file, the text must exist
rb+ Read and write opens 1 text file, allowing read and write, which must exist (in this case, the write file overwrites the previous file)

w opens write only files, zeros the file length if the file exists, that is, the file content disappears, and creates the file if it does not exist
w+ opens the read-write file. If the file exists, the file length will be reset, that is, the file content will disappear. If the file does not exist, the file will be created.
wt opens the writing-only text file. If the file exists, the file length is zeroed, that is, the file content disappears. If the file does not exist, the file is created
wt+ opens a read-write text file. If the file exists, the length of the file is cleared, that is, the content of the file disappears. If the file does not exist, the file is created
wb opens to write only binary files. If the file exists, the file length is zeroed, that is, the content of the file disappears. If the file does not exist, the file is created
wb+ opens the read-write file. If the file exists, the length of the file is reset, that is, the content of the file disappears. If the file does not exist, the file is created

a opens a write-only file as an attachment. If the file does not exist, the file is created. If it does exist, the data is added at the end of the file, that is, the content is appended
a+ opens a read-write file as an add-on, creates a file if it does not exist, and writes data to the end of the file if it does exist (here, you also need to use the rewind() function to read the file, but you do not need the rewind() function to write the file, a is an append).
Append to base at2 data. If it does not exist, it can only be written.
at+ read and write opens a text file, allowing you to read or append data to the end of the text (for the read file, you also need to use the rewind() function, but for the write file you don't need the rewind() function, a is append)
Append to ab2 base data, if it does not exist, it can only be written.
ab+ read and write opens a binary file, creates it if it does not exist, allows you to read or append data at the end of the text (for the read file here, you also need to use the rewind() function, but for the write file you don't need the rewind() function, a is append)

conclusion

The above is the site to introduce C language file operation daquan, I hope to help you, if you have any questions welcome to leave a message, this site will reply you in time!


Related articles: