C language USES text mode and binary mode to open the file distinction analysis

  • 2020-04-02 02:24:39
  • OfStack

Anyone who knows anything about C programming knows that text files and binary files are stored as 0,1 on a computer. For programmers, text and binary files are a declaration of how you should open the file (text/binary), what functions you should use to read and write to the file (read-write), how you should read to the end of the file, and so on.

Specific analysis is as follows:

One, which way to open a file :

ANSI C specifies the standard I/o library, which opens files with the fopen() function. Fopen () function is generally called as:
The FILE * fp.
Fp =fopen(file name, use file mode);
See the following table for the method of using files:

Use file mode meaning
"r" (read-only) Open one for the input The text file
"w" (write) only Open a text file for the output
"a" (additional) Open a text file for appending
"rb" (read-only) Open one for the input binary file
"wb" (write) only Open a binary file for the output
"ab" (additional) Open a binary file for appending
"r+" (reading and writing) Open a text file for read/write
"w+" (reading and writing) Create a text file for read/write
"a+" (reading and writing) Open a text file for read/write
"rb+" (reading and writing) Open a binary file for read/write
"wb+" (reading and writing) Create a binary file for read/write
"ab+" (reading and writing) Open a binary file for read/write
When the same file is read from disk to memory (program data area or cache area), the contents of the memory in the two ways are generally not the same, which is the substantial difference between the two ways of opening.
Just to give you a little bit of context, in Windows, it's going to do a process, which is that when you write a file, the newline is converted to a carriage return, the newline is stored on the disk file, and when you read a file on the disk, it's going to do the inverse process, which is to convert the successive newline returns in the file to a newline.
Therefore, when reading a disk file, text reading to the contents of the file is likely to be shorter than binary, because text reading cuts the file by turning two characters into one character in return for carriage return and line feed. But why is it only possible? Since there may not be two bytes of 45, 42 attached to the text (45 is the ASCII code for CR carriage return and 42 is the ASCII code for line break CL), there is no "truncation" operation, so the reading is the same.
In particular, documents documents (written as text) are best read as text. Binary files (written in binary), preferably read in binary. Otherwise it might not be true.

Two, in what function to read and write files

How data is written to disk is not determined by how the file is opened, but by the write function. How data is read from disk is not determined by how the file is opened, but by the read function.
How do I write this data? How do I store a type of variable? For example, int 12 can be stored directly in binary code of 12 (4 bytes), or in characters 1 and 2.
I'm going to read an int, whether I'm going to read sizeof(int) bytes, or whether I'm going to read a character by character, until the character is not a numeric character.

There are two sets of file read-write functions in C that support exactly the above two ways of reading and writing:

1. Fread (buffer, the size, the count, fp), fwrite (buffer, the size, the count, fp). To read and write a block of data. It corresponds to the first mode of storage. Specifies the number of bytes to read and write directly by the byte length of the type.

2. Fprintf function and fscanf function, which correspond to the second read and write mode. Read and write as characters. The fprintf function and the fscanf function are similar to the printf function and the scanf function in that they are formatted read-write functions. The read and write objects for the fprintf and fscanf functions are disk files, while the read and write objects for the printf and scanf functions are terminals.
Their general call format is:

fprintf( File pointer, format string, output list );
fscanf ( File pointer, format string, input list );

Three, how to judge the end of the document

In the C language, or more precisely, the C standard library, there is a special character called EOF(this definition #define EOF(-1) in stdio.h), which means: end of file. In the while loop, EOF is used as the end flag for the file, and such a file with EOF as the end flag for the file must be a text file. In a text file, the data is stored as ASCII code values of characters. As we know, the ASCII code values range from 0 to 255, making -1 impossible, so we can use EOF as the end of the file.

However, in C, when the data is stored in binary form in a file, the value of -1 appears, and EOF cannot be used as the end of the binary file. To solve this problem, ANSI C provides a feof function to determine if the file is closed. If the end of the file is encountered, the function feof(fp) has a value of 1, otherwise 0.
The feof function can be used to determine whether a binary file ends or a text file ends. However, note that when the feof is used to determine the end of a text file, if the code is not properly written, it may read the EOF in the text as well. The use of the feof function in concrete can refer to http://baike.baidu.com/view/656648.htm.

Four, knowing whether a file is a text file or a binary file, more "reminds" us which read-write function we should choose.

As mentioned earlier, how data is stored is not determined by how the file is opened, but by the read-write function.
For example, if we open a file as a binary (really just to indicate that we are going to convert to a newline), it is more of an idea (virtual) : I "want" the data in this file to be like this, with an int of 4 bytes and a char of 1 byte. In this mode, I read an int into the int variable with fread(buffer,size0f(int),1,fp).

Here's what to remember:

Before we can manipulate a file, we need to know whether it is a text file or a binary file. Files files are opened as text, binary files are opened as binary
If we want to manipulate a binary file, we open it in binary mode (theoretically, we can open it in file mode, but if we write binary data with 45 in it, it will be converted to 45,42 storage, as described above. This is very likely to happen. Fread and fwrite functions are used when reading and writing at the same time.
If I want to work with a text file, we open it as text (or theoretically as binary, but it's not safe). Using, speaking, reading and writing, speaking, reading and writing at the same time the fprintf () function of the characters, fscanf, fgetc, fputc, putw, getw, fgetc, fputs.

Related articles: