What is the difference between reading and writing files in binary and text formats?

Source: Internet
Author: User

What is the difference between reading and writing files in binary and text formats?

In c ++ project development, file read/write operations are often involved. Therefore, let's take a brief look at the differences between the text mode and the binary mode in reading and writing files.

1. read/write of text and binary files on linux

When reading and writing files on linux, the text mode is no different from the binary mode. When reading and writing a file, call fopen to open the file in both text and binary modes, and then read and write the file. The results are the same. Therefore, in linux, files created in binary mode and written files can be correctly read and written in binary or text mode during future access. Likewise, files created in text mode and written files can be correctly read and written in binary or text mode. Take a look at the example:

1.1 open and write files in binary mode

1 // Example 1: Open and write file in binary mode
  2 #include <stdio.h>
  3 #include <iostream>
  4 using namespace std;
  5 int main ()
  6 {
  7 int m = 97;
  8 char s [] = "China \ n";
  9 FILE * fp = fopen ("testBin.txt", "wb"); // Binary mode
10 if (NULL == fp)
11 {
12 return -1;
13}
14 int successCont = fwrite (& m, sizeof (int), 1, fp);
15 if (successCont! = 1)
16 cout << "error" << std :: endl;
17 fwrite (s, sizeof (char), sizeof (s), fp);
18 fprintf (fp, "% d", m); // formatted output
19 fclose (fp);
20 return 1;
twenty one 
twenty two }

The data of testbin.txt is as follows:


Among them: 0061 0000 is the m Value 97 (my computer is a small-End sequence, and the default file encoding is UTF-8), b8e4 e5ad bd9b is the UTF-8 encoding corresponding to Chinese characters, 0 A is \ n, and 3739 is the ASCII value corresponding to characters 7 and 9.

1.2 open and write files in text mode

Modify the 1.1 line of code in section 9th to FILE * fp = fopen ("testTxt.txt", "w"); // text mode. Then re-run the program and upload the testtxt.txt file. The result is as follows:


It can be found that the results of opening and writing files in text mode are the same as those of writing files in binary mode.

 

2. Reading and Writing of text files and binary files on windows

Next, we will briefly compare the differences between reading and writing files in the text and binary modes on the windows platform.

2.1 Open and write files in text mode

If you open a file in "text" mode, the system converts all "/r/n" to "/n" when reading the file. When writing the file, the system converts "/n" to "/r/n. See the following example:

1 #include <stdio.h>
  2 #include <iostream>
  3 using namespace std;
  4 int main ()
  5 {
  6 int m = 97;
  7 char s [] = "China \ n";
  8 FILE * fp = NULL;
  9 fopen_s (& fp, "winTxt.txt", "w");
10 if (NULL == fp)
11 {
12 return -1;
13}
14 int successCont = fwrite (& m, sizeof (int), 1, fp);
15 if (successCont! = 1)
16 cout << "error" << std :: endl;
17 fwrite (s, sizeof (char), sizeof (s), fp);
18 fprintf (fp, "% d", m);
19 fclose (fp);
20 return 1;
twenty one 
twenty two } 

The winTxt.txt result is as follows:


Compared with linux, we can find that \ r (0x0d) is a character before \ n (0x0a ).

 

2.2 Open and write files in binary mode

If the file is opened in binary mode, neither the read/write operations will perform such conversion.

Change the code for line 1 in section 2.1 to: fopen_s (& fp, "winBin.txt", "wb" zookeeper run, and then winBin.txt. The result is as follows:


Therefore, during development on the ipvs platform, files created and written in binary mode are recommended to be read in binary mode to prevent errors in read files. In the same way, the file created and written in text mode. when reading the file, it is recommended to read it in text mode.





Related Article

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.