求文件分割器代码
求文件分割合并代码,急需急需 ,哪位好心的大哥抽个空给我写一下下呗 ,真心感谢了。。。
[解决办法]
分割:
#include <sys\stat.h>
#include <io.h>
#include <fcntl.h>
#include <share.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAX_CLU_BYTES 65536
FILE *fo;
int fh;
__int64 offs,offs1;
__int64 rvi64;
int rv,wrv;
unsigned char buf[MAX_CLU_BYTES];
char ofn[_MAX_PATH];
char offstr[80];
void strcpybutcomma(char *t,char *s) {
char c;
while (1) {
c = *s++;
if (','!=c) *t++ = c;
if (0==c) break;
}
}
void main(int argc,char **argv) {
if (argc<3) {
printf("Copy File Tail.\n");
printf("Usage:\n");
printf(" cft filename.ext offset_begin[-offset_end]\n");
printf("Copy filename.ext offset_begin[-offset_end] to offset_begin[-offset_end]-filename.ext\n");
printf("Note: Byte at offset_end is NOT included.\n");
printf("Example:\n");
printf(" cft abc.rar 12345\n");
printf("Copy abc.rar offset 12345-end to 12345-abc.rar\n");
printf(" cft abc.rar 123-12345\n");
printf("Copy abc.rar offset 123-12345 to 123-12345-abc.rar\n");
printf(" cft abc.rar 0xAB-0xCD\n");
printf("Copy abc.rar offset 0xAB-0xCD to 0xAB-0xCD-abc.rar\n");
return;
}
strcpybutcomma(offstr,argv[2]);
rv=sscanf(offstr,"%I64i-%I64i",&offs,&offs1);
if (rv==0) {
printf("offset %s is not number\n",argv[2]);
return;
}
fh=_sopen(argv[1],_O_BINARY
[解决办法]
_O_RDONLY
[解决办法]
_O_RANDOM,_SH_DENYWR);
if (fh==-1) {
printf("_sopen %s errno=%d\n",argv[1],errno);
return;
}
if (rv==1) {
offs1=_filelengthi64(fh);
if (offs1==-1i64) {
printf("%I64=_filelengthi64 errno=%d\n",offs1,errno);
_close(fh);
return;
}
} else {//rv==2
if (offs1<offs) {
printf("%s offset_begin>offset_end error\n",argv[2]);
_close(fh);
return;
}
}
rvi64=_lseeki64(fh,offs,SEEK_SET);
if (rvi64!=offs) {
printf("%I64u=_lseeki64 %I64u errno=%d\n",rvi64,offs,errno);
_close(fh);
return;
}
sprintf(ofn,"%s-",offstr);
strcat(ofn,argv[1]);
fo=fopen(ofn,"wb");
if (fo==NULL) {
_close(fh);
printf("fopen %s error\n",ofn);
return;
}
cprintf("\n%I64u\r",offs);
while (1) {
rv=_read(fh,buf,(unsigned int)__min(offs1-offs,MAX_CLU_BYTES));
if (rv==0) break;//
if (rv<0) {
fclose(fo);
_close(fh);
printf("_read %s offset %I64u error\n",argv[1],offs);
return;
}
wrv=fwrite(buf,1,rv,fo);
if (wrv!=rv) {
fclose(fo);
_close(fh);
printf("fwrite %s error\n",ofn);
return;
} else {
offs+=rv;
cprintf("%I64u\r",offs);
if (offs>=offs1) break;//
}
}
fclose(fo);
_close(fh);
printf("Copy %s offset %s to %s OK.\n",argv[1],argv[2],ofn);
}
合并:
system("copy /b 文件1+文件2 文件3");[解决办法]
此全程并无判断文件有效性.请自行检查.
分割:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main(int argc, char **argv)
{int n,m;
string name;
cin >> name;
ifstream ifile(name.c_str(),ios_base::binary);
ifile.seekg(0,ios::end);
streampos p=ifile.tellg();
cout << name << "file size is(byte):" <<p << endl;
cout << "block size(byte):" << flush;
cin >> n;
char *b = new char[n];
ifile.seekg(0);
cout << "create file ";
for(int i=0; i<p/n+1; i++){
string s;
ostringstream ss;
ss.fill('0');
ss.width(4);
ss<< i;
cout <<ss.str() << ' ' << flush;
ofstream of(ss.str().c_str(), ios_base::binary);
ifile.read(b ,n);
m = ifile.gcount();
//cout << m << endl;
of.write(b,m);
}
cout << endl;
delete []b;
return 0;
}
合并:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main(int argc, char **argv)
{
string name;
cout << "merge to :" << flush;
cin >> name;
ofstream ofile(name.c_str(),ios_base::binary);
for(int i=0; i<10000; i++){
char c;
string s;
ostringstream ss;
ss.fill('0');
ss.width(4);
ss<< i;
ifstream ifile(ss.str().c_str(), ios_base::binary);
if(!ifile) break;
cout <<ss.str() << ' ' << flush;
while(ifile.get(c)) ofile.put(c);
}
cout << endl;
return 0;
}
[解决办法]
没实际编译链接调试,仅供参考:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char fn1[MAX_PATH];
char fn2[MAX_PATH];
char fn3[MAX_PATH];
char cmd[4*MAX_PATH];
int L;
int main() {
printf("Input fn1:");fflush(stdout);
fgets(fn1,MAX_PATH,stdin);
L=strlen(fn1);
if ('\n'==fn1[L-1]) fn1[L-1]=0;
printf("Input fn2:");fflush(stdout);
fgets(fn2,MAX_PATH,stdin);
L=strlen(fn2);
if ('\n'==fn2[L-1]) fn2[L-1]=0;
printf("Input fn3:");fflush(stdout);
fgets(fn3,MAX_PATH,stdin);
L=strlen(fn3);
if ('\n'==fn3[L-1]) fn3[L-1]=0;
sprintf(cmd,"copy /b "%s"+"%s" "%s"",fn1,fn2,fn3);
system(cmd);
return 0;
}
[解决办法]
当然可以,把for下面删除7行代码.替换成下面的内容.
string f;
cin>> f;
ifstream ifile(f.c_str(), ios_base::binary);
[解决办法]
