C ++ binary file read/write)

Source: Internet
Author: User

Today, I think of these two problems when I was working on the project. So I tested the actual programming and got some new gains:

<1> I always thought I was familiar with how to use the binary files in C/C ++. However, during the test today, I suddenly found that the binary files generated by the program are the same as text files. For example:
File * fp = fopen ("binary", "WB ");
// File * fp = fopen ("character.txt", "W ");
Fprintf (FP, "Count is % d", 250 );
The preceding Code uses text file mode and binary file mode, but the generated binary file still stores the ASCII code. You can directly open it in notepad. The main difference is the line break. The line break of binary file is <LF>, and that of text file is <CR> <LF>, I just want to know what's going on. I obviously opened the file in binary mode. How can I directly save it in ASCII code? That is to say, saving the number 125 still occupies 3 bytes, not just one byte as I imagined!

Later I switched to C ++ and the result was the same. The Code is as follows:
Ofstream FS ("binary", IOS: Binary );
// Ofstream FS ("character.txt ");
Int I = 32765;
FS <I <Endl;
// Fs. Write (char *) & I, 2 );
FS. Close ();
Whether it is opened in binary or text mode, the text is saved in the file! It seems that the binary mode in C/C ++ does not work !?!

After reading the materials, I learned that if I want to output data in binary format in C/C ++, it has nothing to do with the mode when you open the file, the key is that it depends on which function you call to write to the file !! Only fwrite and FS. the write () function can be output in binary format to a file. Calling functions such as puts, fprintf, and <all output ASCII text. In particular, it is similar to the preceding code snippet, in C ++, even if you use the FS <I <Endl statement to output an integer, the output to the binary file is still in text format! <
Operator automatically converts an integer to a single digit before output !! I tried it later. Even if I open a file in text mode, if I use the fwrite function output, the file is still in binary format, it indicates that when the data is output to a file, it does not have to do with the mode in which the file is opened. It is only related to the called output function !!

In addition, you cannot use> to read integers stored in binary format! (Note: I tried it at noon. No! This proves again> only numbers in text format can be read)

(Note: 11: 47am I later thought that all the above things can come down to one sentence: What mode do you use to open a file is not important at all, because you can neither change the content of the file nor the way system functions work in C/C ++, during programming, you only need to care about whether the data content in this file is in binary format or text format! If the content is in text format, you can call the functions in text format, such as puts, gets, fscanf, fprintf, <,>. if the content is in binary format, you can call the functions in the binary format, such as fread, fwrite, and ifstream. read (), ofstream. write.You only need to keep the file content consistent with the processing function. Do not worry about the mode in which the file is opened !!
If you use <to input an integer to a binary file, the data in the text format is actually saved, So you can open it in binary mode, then use> to read the integer. On the contrary, if your binary file contains an integer saved in binary format, you certainly cannot use >> to read the integer in it !!

<2> I want to use a picture to represent the byte sequence:

Today, I finally figured out how to use C ++ to read and write binary files.
The file to be read must contain the <fstream> header file, which contains the C ++ Method for reading and writing files.
You can use the fstream class to read and write files.
1. open the file.
There are two methods to open a file. The first method can use the fstream constructor.
Fstream file ("test. dat", ios_base: In | ios_base: Out | ios_base: APP );
Another method is to use the open function.
Fstream file;
File. Open ("test. dat", ios_base: In | ios_base: Out | ios_base: APP );
In this way, you can open a readable file. If the file does not exist, a new file is created and opened in read/write mode.

Note that, if the file does not exist, the second parameter in the open function must contain ios_base: Out | ios_base: app,
Otherwise, the file cannot be correctly created.
2. Write files.
Advanced file writing operations otherwise it is meaningless to read an empty file.
Since it is a binary file, you can write an integer value to the file. You can only use the write function to write binary characters.
However, the original form of the write function is write (const char * Ch, int size ). The first parameter is of the char * type, so you need to write
Convert the int type of the file to char. The conversion here has plagued me for several days, but I finally figured it out. The Code is as follows.
Int temp;
File. Write (char *) (& temp), sizeof (temp ));
3. Read files.
You can write files, so it is much easier to read files. The READ function is required to read files. The parameter is roughly the same as the write parameter. Read (const char * Ch, int size ).
Reading the content to int type variables also involves a type conversion problem. It is the same as writing a file.
Int readint;
File. Read (char *) (& readint), sizeof (readint ));
In this way, the int value in the file is included in the int variable readint.
4. File pointer.
In the process of reading and writing a file, you often need to read the file selectively. So we need to move the file pointer. This requires the seekg and seekp functions.
In the fstream class, there are two file pointers, one is the pointer to the file to be read, and the other is the pointer to the file to get the pointer position using the tellg and tellp files respectively.
Similarly, seekg and seekp are the two functions that move the two pointers. The parameters of these two functions are the same.
First, the following enumeration types are described:
Ios_base: beg -- start position of the file
Ios_base: cur -- current file location
Ios_base: end -- position at the end of the file

The following uses seekg as an example to describe how to move the pointer:
File. seekg (3) -- move the pointer to the third character
File. seekg (ios_base: Beg) -- move the pointer to the beginning of the file.
File. seekg (ios_base: End) -- move the pointer to the end of the file.
File. seekg (-3, ios_base: cur) -- move the pointer to the current position three characters forward
File. seekg (3, ios_base: cur) -- the current position of the pointer moves three characters backward.
File. seekg (3, file. tellg () -- the current position of the pointer moves three characters backward.
File. seekg (file. tellg () + 3) -- the current position of the pointer moves three characters backward.
5. Do not forget to close the file after the file operation is completed.
File. Close ();

The above five steps complete the file read and write operations. Operations on text files are the same, which is simpler than binary files.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/To9m/archive/2008/11/21/3347773.aspx

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.