"Go" C + + read and write binary files

Source: Internet
Author: User

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
  1. BOOL Copy_binary_file (const char * szdestfile, const char * szorigfile)
  2. {
  3. if (szdestfile = = NULL)
  4. {
  5. return false;
  6. }
  7. if (szorigfile = = NULL)
  8. {
  9. return false;
  10. }
  11. bool BRet = true;
  12. Std::ofstream Fout (szdestfile, std::ios::binary | std::ios::app);
  13. Std::ifstream Fin (Szorigfile, std::ios::binary);
  14. if (Fin.bad ())
  15. {
  16. BRet = false;
  17. }
  18. Else
  19. {
  20. While (!fin.eof ())
  21. {
  22. char szbuf[256] = {0};
  23. Fin.read (Szbuf, sizeof (char) * 256);
  24. if (Fout.bad ())
  25. {
  26. BRet = false;
  27. Break ;
  28. }
  29. //   
  30. Fout.write (Szbuf, sizeof (char) * 256);
  31. }
  32. }
  33. Fin.close ();
  34. Fout.close ();
  35. return bRet;
  36. }

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

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.