花了两个小时,写了一份文件分割合并的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
#include
#include
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"<
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; for(int i = 0; i < n-1 ; i++) pf.read(databuf, size*sizeof(char)); delete [] databuf; ////////////////////////////////////////////////////////////////////////// pf.read(databuf, endlen*sizeof(char)); pf.close(); ////////////////////////////////////////////////////////////////////////// pf1.open(tempstr.c_str(), ios::out|ios::binary|ios::app); databuf = new char[size]; for(int i = 0; i < n-1; i++) pf.read(databuf, size*sizeof(char)); pf.close(); cout << "file :" << tempstr < delete [] databuf; ////////////////////////////////////////////////////////////////////////// temp = pf.tellg(); databuf = new char[flen]; pf.read(databuf, flen*sizeof(char)); pf.close(); return 0;
cout << "number of file is: "<< n <
//////////////////////////////////////////////////////////////////////////
// 先分n-1的文件
char *databuf = new char[size];
{
itoa(i, number, 10);
tempstr = filename + number;
pf1.open(tempstr.c_str(),ios::out|ios::binary);
pf1.write(databuf, size*sizeof(char));
pf1.close();
cout << "file :" << tempstr <
// 分最后一个文件,由于最后一个文件大小不定,所以单独列出来
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];
pf1.write(databuf, endlen*sizeof(char));
pf1.close();
cout << "file :" << tempstr << endl;
delete[] databuf;
// 合并文件类似
tempstr = filename + "A"; //这里为了在同一个目录里面看效果,避免文件同名
{
itoa(i, number, 10);
tempstr = filename + number;
pf.open(tempstr.c_str(), ios::in|ios::binary);
pf1.write(databuf, size*sizeof(char));
// 合并最后一个文件,由于文件大小不清楚,其实没有个文件都可以采用这样的方式
// 合并,都可以避免文件的大小不一的问题,这里假设了出了最后一个文件,其他文
// 件都是相同大小的
itoa(n-1, number, 10);
tempstr = filename + number;
pf.open(tempstr.c_str(), ios::in|ios::binary);
pf.seekg(0, ios_base::end);
flen = pf.tellg();
pf.seekg(temp);
pf1.write(databuf, flen*sizeof(char));
pf1.close();
delete [] databuf;
cout << "file :" << tempstr << endl;
tempstr = filename + "A";
cout << "file :" << tempstr << endl;
//////////////////////////////////////////////////////////////////////////
system("pause");
}