Differences between opening a file in C language and binary mode

Source: Internet
Author: User

We all know that both text files and binary files are stored in 0, 1 on the computer, so how are there differences between the two? I think for programmers, text files and binary files are a declaration that specifies youHow should I open this file (text/binary )?,What function is used to read and write this file (Read and Write Functions), HowJudge the end Of the file.

Specifically: 1. In which way do I open a file? Ansi c specifies the standard input/output function library
The fopen () function opens the file. The fopen () function is called in the following ways:

File * FP;

Fp = fopen (file name, file method );

The following table describes how to use files:

File Usage

Description

"R" (read-only) Open a text file for the input
"W" (write only) Open a text file for the output
"A" (append) Open a text file for append
"Rb" (read-only) Open a binary file for the input
"WB" (write only) Open a binary file for the output
"AB" (append) Open a binary file for append
"R +" (read/write) Open a text file for read/write
"W +" (read/write) Create a text file for read/write
"A +" (read/write) Open a text file for read/write
"RB +" (read/write) Open a binary file for read/write
"WB +" (read/write) Create a binary file for read/write
"AB +" (read/write) Open a binary file for read/write
When a file is read from the disk to the memory (program data area or cache area), the content in the memory is generally different in two ways, that is Substantial differences between the two open methods. Here is a background, that is, in Windows, it will do a processing, that is, when writing a file, the line break will be converted into a carriage return, and the line break will exist in the disk file, when you read a file on a disk, it performs inverse processing, that is, to convert the continuous carriage return and line break in the file into line break. Therefore, when reading a disk file, it is likely that the content of the file to be read in text mode is shorter than that of the binary file, because to read the text mode, you need to press enter and wrap two characters into one character, it 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 CL line break), there will be no "truncation" operation, therefore, the content is the same. Specifically, it is best to read a file (written in text format) in text format. Binary files (written in binary mode), preferably read in binary mode. Otherwise, it may be incorrect. The above has been analyzed. 2. What function is used to read and write files? Writing data to a disk is not determined by the file opening method, but by the write function. Reading data from a disk is not determined by the file opening method, but by the READ function. How to write the data above refers to how a type of variable is stored? For example, int 12 can directly store the binary code of 12 (4 bytes), or can store the character 1, character 2. How to read data refers to whether to read a sizeof (INT) byte directly or a single character or a character until the characters read are not numeric characters. C contains two sets of file read/write functions that support the above two methods: 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. Specify the number of bytes to read and write based on the Type length. 2Fprintf and fscanf correspond to the second read/write method. That is, read and write by character. (Fprintf and fscanf functions are similar to printf and scanf functions. They are all formatted read/write functions. The read and write objects of fprintf and fscanf functions are disk files, while those of printf and scanf functions are terminals .) They are generally called in the following format:

Fprintf (File pointer, Format String, output list );

Fscanf (File pointer, Format String, input list );

3How to determine the end of a file?

In the C language, or more accurately speaking of the C standard library, there is a special character EOF (stdio. h In this definition
# Define EOF (-1)
), ItEnd of file ). In the while loop, EOF is used as the end mark of the file, which must be a text file. In text files, data is stored in the form of ASCII code values of characters. We know that the ASCII code value ranges from 0 ~ 255.-1 is not possible, so EOF can be used as the end mark of the file.

However,In C language, when data is stored in a binary file, the value-1 is displayed. In this case, 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 is finished. If the file ends, the value of the feof (FP) function is 1; otherwise, the value is 0.

The feof function can be used to determine whether a binary file ends or whether a text file ends. However, when feof is used to determine the end of a text file, if the code is improperly compiled, the file Terminator EOF in the text may also be read. For details, seeHttp://baike.baidu.com/view/656648.htm

4. Know whether a file is a text file or a binary file. For more "reminders", which read/write function should be selected.

As mentioned in 2, how to store data is not determined by the file opening method, but by the read/write function.

For example, we open a file in the form of a binary file (in fact, it only specifies the conversion of line breaks), it is more representative of a concept (virtual ): I "want" the data in this file to be like this. The Int type occupies 4 bytes and the char occupies 1 byte. In this mode, I use fread (buffer, size0f (INT), 1, FP) to read an int into the int variable.

Remember

Before operating a file, we need to know whether the file is a text file or a binary file. File files are opened in text mode, and binary files are opened in binary mode.

If we want to operate a binary file, we can open it in binary mode (theoretically, we can also open it in file mode, but if the written binary data contains45, will be converted to 45, 42 storage, see 1. This is very likely to happen ). Fread and fwrite functions are used for reading and writing at the same time.

If I want to operate a text file, we can open it in text mode (theoretically, it can be opened in binary mode, but it is not safe ). Functions that read and write characters at the same timeFprintf, fscanf
, Fgetc, fputc, putw, getw, fgetc, fputs.

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.