【转】C++读写二进制文件

原文网址:http://blog.csdn.net/lightlater/article/details/6364931

摘要:

使用C++读写二进制文件,在开发中操作的比较频繁,今天有幸找到一篇文章,遂进行了一些试验,并进行了部分的总结。

 

使用C++操作文件,是研发过程中比较频繁的,因此进行必要的总结和封装还是十分有用的。今天在网上找到一篇,遂进行了部分的试验,以记之,备后用。

 

本文读写文件均使用文件流进行操作,主要使用的类是ifstream, ofstream, 使用时,请务必包含文件fstream。如下:

#include <fstream>

 

 

写二进制文件

写二进制文件应该使用ofstream类,文件的打开模式一定要是 binary,如果传入的不是 binary, 文件将以ASCII方式打开。

 

下面是示例代码,用于写入文件。

 

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();

 

而写文本文件则比较简单,如下:

 

std::ofstream fout("b.dat");

 

int nNum = 20;

std::string str("Hello, world");

 

fout << nNum << "," << str << std::endl;

 

fout.close();

  

 

读二进制文件

读取二进制文件可以使用ifstream 类来进行,文件的打开模式一定要是 binary,如果传入的不是 binary, 文件将以ASCII方式打开。

 

下面是示例代码:

 

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();

 

而读取文本文件则比较简单:

 

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();

 

文件的打开模式

文件操作时,如果不显示指定打开模式,文件流类将使用默认值。

在<fstream> 中定义了如下打开模式和文件属性:

 ios::app // 从后面添加

 ios::ate // 打开并找到文件尾

 ios::binary // 二进制模式I/O(与文本模式相对)

 ios::in // 只读打开

 ios::out // 写打开

 ios::trunc // 将文件截为 0 长度

可以使用位操作符 OR 组合这些标志,比如

 ofstream logFile("log.dat", ios::binary | ios::app);

 

 

二进制文件的复制

这里我实现了一个二进制文件的复制操作,用于验证读写的正确性,示例代码如下:

 

[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.   
  8.     if (szOrigFile == NULL)  
  9.     {  
  10.         return false;  
  11.     }  
  12.   
  13.     bool bRet = true;  
  14.   
  15.     std::ofstream fout(szDestFile, std::ios::binary | std::ios::app);  
  16.     std::ifstream fin(szOrigFile, std::ios::binary);  
  17.   
  18.     if (fin.bad())  
  19.     {  
  20.         bRet = false;  
  21.     }  
  22.     else  
  23.     {  
  24.         while(!fin.eof())  
  25.         {  
  26.             char szBuf[256] = {0};  
  27.               
  28.             fin.read(szBuf, sizeof(char) * 256);  
  29.               
  30.             if (fout.bad())  
  31.             {  
  32.                 bRet = false;  
  33.                 break;  
  34.             }  
  35.   
  36.             //   
  37.             fout.write(szBuf, sizeof(char) * 256);  
  38.         }  
  39.     }  
  40.   
  41.     fin.close();  
  42.     fout.close();  
  43.   
  44.     return bRet;  
  45. }  

 

 

 

后记

由于文本文件本质上也是磁盘上的一个个二进制编码,因此,读写二进制文件的代码同样可以读写文本文件,在文件类型不是很明确的读写操作中,直接使用二进制读写比较可取,如果可以直接判断文件类型,则可以分别对待。

关于读取文本文件,请参照http://blog.csdn.net/lightlater/archive/2011/04/15/6326338.aspx

 

 

 

参考文献:

* 用C++进行简单的文件I/O操作(http://www.vckbase.com/document/viewdoc/?id=1439 )

* 百度文库(http://wenku.baidu.com/view/9faa23db50e2524de5187eb3.html )

posted on 2015-09-24 14:26  wi100sh  阅读(373)  评论(0编辑  收藏  举报

导航