Original URL: http://blog.csdn.net/lightlater/article/details/6364931
Summary:
The use of C + + read and write binary files, in the development of more frequent operation, today was fortunate to find an article, then carried out a number of experiments, and carried out a part of the summary.
The use of C + + operation files is more frequent in the development process, so it is very useful to carry out the necessary summary and encapsulation. Today on the Internet to find an article, and then carried out a part of the experiment to remember, ready for use.
Read and write files are used in the file stream operation, the main use of the class is Ifstream, Ofstream, when used, be sure to include the file fstream. As follows:
#include <fstream>
Write a binary file
Write binary files should use the Ofstream class, the open mode of the file must be binary, if not binary, the file will be opened in ASCII format.
The following is a sample code for writing to a file.
Std::ofstream fout ("A.dat", std::ios::binary);
int nnum = 20;
std::string Str ("Hello, World");
Fout.write ((char*) &nnum, sizeof (int));
Fout.write (Str.c_str (), sizeof (char) * (Str.size ()));
Fout.close ();
Writing a text file is relatively straightforward, as follows:
Std::ofstream fout ("B.dat");
int nnum = 20;
std::string Str ("Hello, World");
Fout << nnum << "," << str << Std::endl;
Fout.close ();
Read binary files
Reading binary files can be done using the Ifstream class, the open mode of the file must be binary, if the incoming is not binary, the file will be opened in ASCII format.
Here is the sample code:
Std::ifstream fin ("A.dat", std::ios::binary);
int nnum;
Char szbuf[256] = {0};
Fin.read ((char*) &nnum, sizeof (int));
Fin.read (szbuf, sizeof (char) * 256);
Std::cout << "int =" << nnum << Std::endl;
Std::cout << "str =" << szbuf << Std::endl;
Fin.close ();
While reading a text file is easier:
Std::ifstream fin ("b.dat");
int nnum;
Char szbuf[256] = {0};
Fin >> nnum >> szbuf;
Std::cout << "int =" << nnum << Std::endl;
Std::cout << "str =" << szbuf << Std::endl;
Fin.close ();
Open mode of File
When a file operation is not displayed, the file stream class uses the default value if it does not display the specified open mode.
The following open mode and file attributes are defined in <fstream>:
Ios::app//Add from behind
Ios::ate//Open and find end of file
Ios::binary//Binary mode I/O (in relation to text mode)
Ios::in//read-only Open
Ios::out//write Open
Ios::trunc//Truncate the file to 0 length
These flags can be combined using a bitwise operator OR, for example,
Ofstream logFile ("Log.dat", Ios::binary | ios::app);
copy of binary files
Here I implemented a binary file copy operation to verify the correctness of read and write, the sample code is as follows:
[CPP]View Plaincopy
- BOOL Copy_binary_file (const char * szdestfile, const char * szorigfile)
- {
- if (szdestfile = = NULL)
- {
- return false;
- }
- if (szorigfile = = NULL)
- {
- return false;
- }
- bool BRet = true;
- Std::ofstream Fout (szdestfile, std::ios::binary | std::ios::app);
- Std::ifstream Fin (Szorigfile, std::ios::binary);
- if (Fin.bad ())
- {
- BRet = false;
- }
- Else
- {
- While (!fin.eof ())
- {
- char szbuf[256] = {0};
- Fin.read (Szbuf, sizeof (char) * 256);
- if (Fout.bad ())
- {
- BRet = false;
- Break ;
- }
- //
- Fout.write (Szbuf, sizeof (char) * 256);
- }
- }
- Fin.close ();
- Fout.close ();
- return bRet;
- }
Postscript
Because the text file is essentially a binary encoding on the disk, so the code read and write binary files can also read and write text files, in the file type is not very clear read and write operations, the direct use of binary read and write is preferable, if you can directly determine the file type, you can be treated separately.
For reading text files, refer to Http://blog.csdn.net/lightlater/archive/2011/04/15/6326338.aspx
Reference documents:
* Simple File I/O operation in C + + (http://www.vckbase.com/document/viewdoc/?id=1439)
* Baidu Library (http://wenku.baidu.com/view/9faa23db50e2524de5187eb3.html)
"Go" C + + read and write binary files