C language using text and binary mode to open the difference between the file analysis _c language

Source: Internet
Author: User
Tags fread truncated

People who know a little about C programming know that text files and binaries are stored in 0,1 on a computer, so how are the two different? For programmers, text files and binaries are statements that indicate how you should open the file (text/binary), what functions to read and write to the file (read-write function), and how to read the end of the file.

The specific analysis is as follows:

first, in which way to open a file :

ANSI C prescribes the standard input output function library, which opens the file with the fopen () function. The fopen () function is invoked in a general way:
FILE *FP;
Fp=fopen (filename, use file method);
Use the file method to see the following table:

Use file mode Meaning
"R" (Read Only) Open a text file for input
"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 the output
"AB" (Append) Open a binary file for append
"R+" (Read and write) Open a text file for read/write
"w+" (Read and write) Create a text file for read/write
"A +" (Read and write) Open a text file for read/write
"Rb+" (Read and write) Open a binary file for read/write
"Wb+" (Read and write) Create a binary file for read/write
"Ab+" (Read and write) Open a binary file for read/write

When the same file reads files from disk to memory (program data area or buffer), in two ways, the contents of memory are not the same, which is the substantive difference between the two ways of opening.
Here's a background, that is, under Windows, it will do a deal, is to write a file, line breaks will be converted to carriage return, line breaks exist on disk files, and read the file on the disk, it will be reversed processing, that is, the file in a continuous return, line-feed characters into line-feed characters.
Therefore, when reading a disk file, the text reading to the file content is likely to be shorter than the binary file, because the text is read to send a carriage return, line two characters into a character, equivalent to truncated file. But why is it just possible? Because it is possible that the text does not have a connected 45,42 two bytes (45 is the ASCII of CR carriage return, 42 is the ASCII code of the newline character Cl), there is no "truncated" operation, so the content read is the same.
Specifically, file files (written in text) are best read in text format. binary files (written in binary format), preferably in binary mode. Otherwise it may not be correct.

Second, what function to read and write files

How data is written on 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 the way the file is opened, but by the read function.
How does the data written above mean, how does a variable of a type exist? For example, int 12, you can deposit 12 of the binary code (4 bytes), can also save characters 1, character 2.
The data how to read is that I want to read an int variable, is read directly sizeof (int) byte, or a character of a character read until the read character is not a numeric character.

c There are two sets of file read and write functions that support the above two ways to read and write:

1.fread (BUFFER,SIZE,COUNT,FP), fwrite (BUFFER,SIZE,COUNT,FP). Used to read and write a block of data. It corresponds to the first type of storage. Specifies the number of bytes read and write directly by the byte length of the type.

The 2.fprintf function and the FSCANF function. It corresponds to the second way of reading and writing. Read and write in the form of characters. (The fprintf function, the FSCANF function is similar to the printf function, the scanf function, are formatted read-write functions.) The Read and write objects of the fprintf and FSCANF functions are disk files, while the read and write objects of printf and scanf functions are terminals. )
Their general calling format is:

fprintf (file pointer, format string, output list);
FSCANF (file pointer, format string, input list);

Third, how to judge the end of documents

In the C language, or more precisely in the C standard function library, there is a special character eof (this definition #define EOF (-1) in stdio.h), which indicates: end of file. With EOF as the end of file in the while loop, this file with EOF as the end of the file must be a text file. In a text file, the data is stored in the ASCII code value of the character. We know that the range of ASCII code values is 0~255, which is not possible-1, so you can use EOF as a file end flag.

However, in the C language, when the data in binary form into the file, there will be a 1 value of the occurrence, at this time can not use EOF as the end of the binary file flag. To solve this problem, ANSI C provides a feof function to determine whether the file is finished. If the end of the file is encountered, the value of function feof (FP) is 1, otherwise 0.
The feof function can be used both to determine whether the binary file is finished or to determine whether the text file ends. However, note that feof used to determine the end of a text file, if the code is not written incorrectly, may be the text of the end of the file in EOF also read out, you can refer to the use of the feof function in http://baike.baidu.com/view/656648.htm.

Four, know a file is a text file, or binary file, more "remind" us, should choose which read and write function.

As mentioned earlier, how the data is saved is not determined by the way the file is opened, but by the read-write function.
For example, we open a file as a binary file (actually just to show the conversion of the line break), it is more of a concept (virtual): I hope the data in this file is like this, the type of int is 4 bytes, char is 1 bytes. In this mode, I read an int to an int variable with fread (buffer,size0f (int), 1,FP).

Here's what you need to remember:

Before we operate on a file, first, we need to know whether the file is a text file or a binary file. The file file is opened in text mode, and binary files are opened in binary mode.
If we're going to manipulate a binary file, we'll open it in binary mode (theoretically it can also be opened in a file, but if you write binary data that has 45 o'clock in it, it will be converted to 45, 42 storage, as described earlier in this article.) This is very likely to happen). When reading and writing at the same time use fread,fwrite these two functions.
If I want to manipulate a text file, we'll open it as text (theoretically it can be opened in binary form, but not insured). While reading and writing, those functions are fprintf,fscanf, fgetc,fputc,putw,getw,fgetc,fputs, and read-write characters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.