c++ 文件拆分合并(转载)

花了两个小时,写了一份文件分割合并的demo代码

只供学习研究用

用到了stl的 fstream, iostream, string

fstream对文件进行操作,iostream控制输入输出流,string对字符串进行操作

这里我只在开始的检查了文件是否打开的问题,在后面的代码中都没有检查,只是demo的原因,应该在每次打开文件的时候,都必须检查文件是否打开

以下为代码:

/********************************************************************
 created: 2005/08/18  14:02
 created: 18:8:2005   16:22
 filename:  e:/work/Me/TempTest/cstringTest/cstringTest/cstringTest.cpp
 file path: e:/work/Me/TempTest/cstringTest/cstringTest
 file base: cstringTest
 file ext: cpp
 author:  赵开勇
 
 purpose: 分割文件,合并文件,现在是固定大小
    如果大小有变化,size有变化,可以采用多次的访问的方式来处理文件
    既是每一次只操作一小部分
*********************************************************************/


#include <string>
#include <iostream>
#include <fstream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 fstream pf,pf1;
 string filename("E://D100.mpg");  // 这里限定了文件名,也是只是demo而已
 string tempstr = "";
 char number[10];

 pf.open(filename.c_str(),ios::in|ios::binary);
 
 if (!pf)
 {
  cout<< "err"<<endl;
 }

 int temp = pf.tellg();
 pf.seekg(0, ios_base::end);
 long flen = pf.tellg();
 pf.seekg(temp);
 
 cout << "file size is: " << flen <<" byte"<< endl;

 static const long size = 1024000;   // 这里限定了文件的分割大小,只是demo而已

 int n = flen / size + 1;  // 文件要分为多少分,为了保证最有一个文件最小,故+1;
 cout << "number of file is: "<< n <<endl;


//
// 先分n-1的文件

 char *databuf = new char[size];

 for(int i = 0; i < n-1 ; i++)
 {
  itoa(i, number, 10);
  tempstr = filename + number;
  pf1.open(tempstr.c_str(),ios::out|ios::binary);

  pf.read(databuf, size*sizeof(char));
  pf1.write(databuf, size*sizeof(char));
  pf1.close();
  cout << "file :" << tempstr <<endl; 
 }

 delete [] databuf;

//
// 分最后一个文件,由于最后一个文件大小不定,所以单独列出来

 long endlen = flen - size * (n-1);
 
 itoa(n-1, number, 10);
 tempstr = filename + number;
 pf1.open(tempstr.c_str(),ios::out|ios::binary);
 
 databuf = new char[endlen];

 pf.read(databuf, endlen*sizeof(char));
 pf1.write(databuf, endlen*sizeof(char));
 pf1.close();
 cout << "file :" << tempstr << endl;

 pf.close();
 delete[] databuf;

//
// 合并文件类似

 tempstr = filename + "A";    //这里为了在同一个目录里面看效果,避免文件同名

 pf1.open(tempstr.c_str(), ios::out|ios::binary|ios::app);

 databuf = new char[size];

 for(int i = 0; i < n-1; i++)
 {
  itoa(i, number, 10);
  tempstr = filename + number;
  pf.open(tempstr.c_str(), ios::in|ios::binary);

  pf.read(databuf, size*sizeof(char));
  pf1.write(databuf, size*sizeof(char));

  pf.close();

  cout << "file :" << tempstr <<endl;
 }

 delete [] databuf;

//
// 合并最后一个文件,由于文件大小不清楚,其实没有个文件都可以采用这样的方式
// 合并,都可以避免文件的大小不一的问题,这里假设了出了最后一个文件,其他文
// 件都是相同大小的

    
 itoa(n-1, number, 10);
 tempstr = filename + number;
 pf.open(tempstr.c_str(), ios::in|ios::binary);

 temp = pf.tellg();
 pf.seekg(0, ios_base::end);
 flen = pf.tellg();
 pf.seekg(temp);

 databuf = new char[flen];

 pf.read(databuf, flen*sizeof(char));
 pf1.write(databuf, flen*sizeof(char));

 pf.close();
 pf1.close();
 delete [] databuf;
 cout << "file :" << tempstr << endl;


 tempstr = filename + "A";
 cout << "file :" << tempstr << endl;
//
    system("pause");

 return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值