The file types that the c language can handle are: text files and binary files. The files that C language can process are divided into text files and binary files according to the storage form: 1. Text files store an ASCII code, and the content of the file can be directly input and output; 2. Binary files directly store characters, and binary files cannot be stored. The contents of the file are output directly to the screen.
The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.
In computers, input and output are in the form of data streams. According to the access mode, files are divided into sequential access files and random access files. According to the storage form, it is divided into binary files and text files. The text file stores an ASCII code, and the content of the file can be directly input and output. Binary files store characters directly, and the contents of binary files cannot be output directly to the screen. Therefore, the files thatC language can process are divided into text files and binary files according to the storage form.
The difference between text files and binary files
Text files are usually used to save characters visible to the naked eye, such as .txt files, .c files, and .dat files, etc., open these files with a text editor, and we can successfully understand the contents of the files.
Binary files are usually used to save unreadable content such as videos, pictures, programs, etc. If you open these files with a text editor, you will see a bunch of garbled characters that you cannot understand at all.
But physically speaking, there is no difference between binary files and character files. They are both data stored on the disk in binary form.
The reason why we can understand the content of text files is because the text files use character encodings such as ASCII, UTF-8, GBK, etc. The text editor can recognize these encoding formats and convert the encoding values to Convert to characters and display them.
The binary files use special encoding formats such as mp4, gif, exe, etc. The text editor does not recognize these encoding formats and can only parse them randomly according to the character encoding format, so it becomes a bunch of messy characters. , some have never even seen it.
If we create a new mp4 file, write a string of characters to it, and then open it with a text editor, you can still understand it. Interested readers can try it themselves.
To sum up, different types of files have different encoding formats, and corresponding programs (software) must be used to parse them correctly, otherwise they will be a bunch of garbled characters or cannot be used.
For programmers, text files and binary files are a statement, indicating how you should open the file (text mode/binary), what function you should use to read and write this file (read and write functions), how The judgment reads to the end of this file.
Specifically:
1. How to open a file?
ANSI C specifies the standard input and output function library, and uses the fopen() function to open files. The calling method of the fopen() function is generally:
FILE *fp; fp=fopen(文件名,使用文件方式);
See the table below for using the file method:
When the same file is read from the disk to the memory (program data area or cache area), the contents in the memory are generally different in the two methods. This is the substantial difference between the two opening methods.
There is a background here, that is, under Windows, it will do a process, that is, when writing a file, the newline character will be converted into a carriage return. The newline character is stored in the disk file, and when reading the file, the newline character will be converted into a carriage return. file, it will perform reverse processing, that is, convert consecutive carriage returns and line feeds in the file into line feeds.
Therefore, when reading a disk file, the file content read in text mode is likely to be shorter than the binary file, because reading in text mode requires two characters of carriage return and line feed to be converted into one character. , which is equivalent to truncating the file. But why is it just possible? Because there may not be two consecutive bytes 45 and 42 in the text (45 is the ASCII code of CR carriage return, 42 is the ASCII code of line feed CL), there is no "truncation" operation, so reading The content is the same.
Specifically, file files (written in text mode) are best read in text mode. Binary files (written in binary mode) are best read in binary mode. Otherwise it may be incorrect. The above has been analyzed.
2. What function is used to read and write files?
How data is written on the disk is not determined by the file opening method, but by the writing function. How data is read from the disk is not determined by the file opening method, but by the read function.
How to write data mentioned above refers to how to store a type of variable? For example, int 12, you can directly store the binary code of 12 (4 bytes), or you can store character 1 and character 2.
How to read data means that if I want to read an int variable, I can read it directly. sizeof (int) bytes, or read character by character until the character read is not a numeric character.
There are two sets of file reading and writing functions in C that support the above two methods of reading and writing:
1.fread(buffer,size,count,fp), fwrite(buffer,size,count,fp). Used to read and write a data block. It corresponds to the first storage method. Directly specify the number of bytes to read and write according to the byte length of the type.
2fprintf function and fscanf function. It corresponds to the second reading and writing method. That is, reading and writing in character format. (The fprintf function and the fscanf function have similar functions to the printf function and the scanf function. They are all formatted reading and writing functions. The reading and writing objects of the fprintf and fscanf functions are disk files, while the reading and writing objects of the printf and scanf functions are the terminal.)
Their general calling format is:
fprintf (file pointer, format string, output list);
fscanf (file pointer, format string, input list);
3 How to determine the end of the file?
In the C language, or more precisely the C standard function library, there is a special character EOF (this definition in stdio.h #define EOF (-1) ), which means :end of file. In the while loop, EOF is used as the end-of-file mark. The file with EOF as the end-of-file mark must be a text file. In text files, data is stored in the form of character ASCII code values. We know that the range of ASCII code values is 0~255, and -1 is impossible, so EOF can be used as the end of file mark.
However, in C language, when data is stored in a file in binary form, a -1 value will appear. At this time, EOF cannot be used as the end mark of the binary file. To solve this problem, ANSI C provides a feof function to determine whether the file has ended. If the end of the file is encountered, the value of the function feof (fp) is 1, otherwise it is 0.
The feof function can be used to determine whether the binary file ends or the text file ends. However, it should be noted that when feof is used to determine the end of a text file, if the code is not written properly, the end-of-file character EOF in the text may also be read; see http://baike.baidu.com/view/656648 for details. htm
4. Knowing whether a file is a text file or a binary file will "remind" us more about which read and write function we should choose.
As mentioned in 2, how the data is stored is not determined by the file opening method, but by the read and write functions.
For example, if we open a file as a binary file (actually only specifying the conversion of newline characters), it represents more of an idea (virtual): I "hope" The data in this file is like this, int type occupies 4 bytes and char occupies 1 byte. In this mode, I use fread(buffer,size0f(int),1,fp) to read an int into an int variable.
We remember
Before we operate on a file, first of all, we must know whether the file is a text file or a binary file. Files are opened in text mode, binary files are opened in binary mode
If we want to operate a binary file, then we open it in binary mode (theoretically it can also be opened in file mode, but if the binary data is written When there is 45 in it, it will be converted into 45,42 and stored, see 1. This is very likely to happen). When reading and writing at the same time, use the two functions fread and fwrite.
If I want to operate a text file, then we open it in text mode (theoretically it can also be opened in binary mode, but it is not safe). When reading and writing at the same time, use those functions fprintf, fscanf, fgetc, fputc, putw, getw, fgetc, fputs
Related recommendations: "C Video Tutorial"
Using the file method |
Meaning |
"r" (read-only) |
Opens a text for input File |
"w" (write only) |
Open a text file for output |
"a" (append) |
Open a text file for append |
"rb" (read only) |
Open a binary file for input |
"wb" (write only) |
Open a binary file for output |
"ab" (append) |
Open a binary file for append |
"r "(read and write) |
Open a text file for reading/writing |
"w "(reading and writing) |
for reading/writing Create a text file |
"a " (read and write) |
Open a text file for reading/writing |
"rb "(read and write) |
Open a binary file for reading/writing |
"wb "(read and write) |
Create a binary file for reading/writing |
##"ab "(read and write) | Open a binary file for reading/writing |
The above is the detailed content of What are the file types that C language can handle?. For more information, please follow other related articles on the PHP Chinese website!