怎样将生成的40M大小的文件分段写到四个文件中,每个文件大小10M解决方法
怎样将生成的40M大小的文件分段写到四个文件中,每个文件大小10M
我利用CopyFile能够将其全部拷贝(代码如下),但是不知道怎样分段拷贝.请同志们指点指点下我! 谢谢了
FILE *fppattern;
sprintf(srcFile, "%s\\test ", srcFile);
if (fppattern = fopen(srcFile, "wb "))
{
msg.Format( "Create File %s\r\n ", srcFile);
TestLog_HardDisk(strLog,msg.GetBuffer());
//64*160*4 = 64k * 160 * 4 = 10M * 4 = 40M
for (int i=0; i < 65536*160*4; i++)
{
int x = rand();
fwrite(&x, sizeof(int), 1, fppattern);
}
fclose(fppattern);
}
//copy the whole
sprintf(dstFile, "%s1 ", dstFile);
if (CopyFile(srcFile, dstFile, false) == 0)
{
return false;
}
------解决方案--------------------
自己编一函数吧
#include <stdio.h>
const int sizeofpart=65536*160;
bool copy_part(int part,FILE *in,FILE *out)
{
char *data=new char[sizeofpart+10];
if(fseek(in,part*sizeofpart,SEEK_SET)==-1)
return false;
fread(data,1,sizeofpart,in);
fwrite(data,1,sizeofpart,out);
delete []data;
return true;
}
void main()
{
FILE *src,*part;
char filename[256];
src=fopen( "test ", "r ");
int i;
for(i=0;i <4;i++)
{
sprintf(filename, "part%d ",i+1);
part=fopen(filename, "w ");
rewind(part);
if(!copy_part(i,src,part))
break;
fclose(part);
}
if(i <4)
printf( "Fail!\n ");
fclose(src);
}
------解决方案--------------------
不要用copyfile,用文件分割合并
FILE *fpread, *fpwrite;
fpsrc = fopen(srcFile, "rb ")
fpdst = fopen(dstfile, "wr ");
fseek(fpsrc,npos,SEEK_SET);//然后用fseek定位开始读的位置,
fread(buffer,1,nsize,fpsrc);//读nsize bytes 到buffer
fwrite(buffer,1,nsize,fpdst);//将读出的数据写到fpdst
//文件读取方式为二进制
我利用CopyFile能够将其全部拷贝(代码如下),但是不知道怎样分段拷贝.请同志们指点指点下我! 谢谢了
FILE *fppattern;
sprintf(srcFile, "%s\\test ", srcFile);
if (fppattern = fopen(srcFile, "wb "))
{
msg.Format( "Create File %s\r\n ", srcFile);
TestLog_HardDisk(strLog,msg.GetBuffer());
//64*160*4 = 64k * 160 * 4 = 10M * 4 = 40M
for (int i=0; i < 65536*160*4; i++)
{
int x = rand();
fwrite(&x, sizeof(int), 1, fppattern);
}
fclose(fppattern);
}
//copy the whole
sprintf(dstFile, "%s1 ", dstFile);
if (CopyFile(srcFile, dstFile, false) == 0)
{
return false;
}
------解决方案--------------------
自己编一函数吧
#include <stdio.h>
const int sizeofpart=65536*160;
bool copy_part(int part,FILE *in,FILE *out)
{
char *data=new char[sizeofpart+10];
if(fseek(in,part*sizeofpart,SEEK_SET)==-1)
return false;
fread(data,1,sizeofpart,in);
fwrite(data,1,sizeofpart,out);
delete []data;
return true;
}
void main()
{
FILE *src,*part;
char filename[256];
src=fopen( "test ", "r ");
int i;
for(i=0;i <4;i++)
{
sprintf(filename, "part%d ",i+1);
part=fopen(filename, "w ");
rewind(part);
if(!copy_part(i,src,part))
break;
fclose(part);
}
if(i <4)
printf( "Fail!\n ");
fclose(src);
}
------解决方案--------------------
不要用copyfile,用文件分割合并
FILE *fpread, *fpwrite;
fpsrc = fopen(srcFile, "rb ")
fpdst = fopen(dstfile, "wr ");
fseek(fpsrc,npos,SEEK_SET);//然后用fseek定位开始读的位置,
fread(buffer,1,nsize,fpsrc);//读nsize bytes 到buffer
fwrite(buffer,1,nsize,fpdst);//将读出的数据写到fpdst
//文件读取方式为二进制