C language file open mode

  • 2020-06-03 07:48:41
  • 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 the file, sometimes messy code will be generated. At this time, you should check 1 of your own program, 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 in 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 the 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 1 base 2 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 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

w+ opens the read-write file. If the file exists, the file length will be reset, i.e. the file content will disappear. If the file does not exist, the file will be created.

wt opens a writing-only text file. 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

wt+ opens a read-write text file. If the file exists, the file length is cleared, that is, the file content disappears, and if the file does not exist, the file is created

wb opens write only 2 base files. 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

wb+ opens a read-write 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

a opens a write-only file as an add-on, creates a file if one does not exist, and appends content if one does

a+ opens the read-write file in an additional way, creates the file if it does not exist, and writes the data to the end of the file if it does exist (for the read file here, the rewind() function is also needed, but rewind() function is not needed for the write file, a is appended)

Append to at2 base data, if it does not exist, it can only be written.

at+ read/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/write opens a binary file, creates it if it doesn't exist, allows you to read or append data at 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)


Related articles: