using namespace std
string filename("E:\\D100.mpg");
pf.open(filename.c_str(),ios::in|ios::binary);
pf.seekg(0, ios_base::end);
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 < }
delete [] databuf;
特别是这一段 pf1.open(tempstr.c_str(),ios::out|ios::binary);还有pf.read(databuf, size*sizeof(char));
以及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;
以上的程序片断完全看不懂,希望各位大虾指教,程序是用来分割一个文件,完整代码如下
int _tmain(int argc, _TCHAR* argv[])
{
fstream pf,pf1;
string filename("E:\\D100.mpg"); // 这里限定了文件名,为了方便起见
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; // 这里限定了文件的分割大小,也是为了方便起见;
int n = flen / size + 1; // 文件要分为多少分,为了保证最后一个文件最小,故+1;
cout << "number of file is: "<< n <
//////////////////////////////////////////////////////////////////////////
// 先分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 < }
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;
}
--------------------next---------------------
阅读(1609) | 评论(0) | 转发(0) |