/*******************************************
文件名:splitf.c
作者:发哥
版本历史:2008-12-7 V1.0
********************************************/
#include
#include
#define BIT_SIZE 1024
#define NAME_ESIZE 20
void main(long argc,char*argv[])//splitf.exe /s [1024] afile.dat
{
long getNumber(char*p);//获取[]里的数字
void getName(char*p,char firstInFileName[],char lastInFileName[]);//获取文件名
char getChoose(char*p);//选择的字母
void splitf(char firstInFileName[],char lastInFileName[],long number);//剪切函数
void combination(char firstInFileName[],char lastInFileName[],long number);//合并函数
long number,i=1;
char firstInFileName[NAME_ESIZE],lastInFileName[NAME_ESIZE],choose;
if(argc!=4)
{
printf("请输入如下格式:\n splitf.exe /s [数字] afile.dat\n或者:splitf.exe /m [数字] afile.dat\n");
return 0;
}
number=getNumber(argv[2]);
choose=getChoose(argv[1]);
getName(argv[3],firstInFileName,lastInFileName);
if(choose=='s') splitf(firstInFileName,lastInFileName,number);
else if(choose=='m') combination(firstInFileName,lastInFileName,number);
else
{
printf("请输入如下格式:splitf.exe /s [数字] afile.dat\n或者:splitf.exe /m [数字] afile.dat\n");
return 0;
}
}
long getNumber(char*p)
{
long number;
char *sNumber=(char *)malloc(NAME_ESIZE);
strcpy(sNumber,p);
sNumber[0]='0';
sNumber[strlen(sNumber)-1]=0;
number=atoi(sNumber);
free((void*)sNumber);
return number;
}
void getName(char*p,char firstInFileName[],char lastInFileName[])
{
long j,i,len,point;
len=strlen(p);
for(i=len-1,point=0;i>=0;i--)
if(*(p+i)=='.') point=i;
if(point==0||point==len-1){printf("文件名应包含扩展名!\n");return;}
for(i=0;i
firstInFileName[i]=*(p+i);
firstInFileName[i]=0;
for(i=point+1,j=0;i<=len;i++,j++)
lastInFileName[j]=*(p+i);
}
char getChoose(char*p)
{
char ch;
if(strlen(p)!=2||*p!=47)//斜杠
{
printf("输入错误!\n");
return 0;
}
ch=*(p+1);
return ch;
}
void splitf(char firstInFileName[],char lastInFileName[],long num)
{
char *buffer,*fileName;
long i=1,fileSize,n,filecount,sizecount,j=1,number;
FILE *in,*out;
if(num<2){printf("输入数字应大于1!\n");return;}
fileName=(char*)malloc(NAME_ESIZE);
sprintf(fileName,"%s.%s",firstInFileName,lastInFileName);
in=fopen(fileName,"rb");
if(!in) {printf("找不到指定文件\n");return;}
fseek(in,0,2);
fileSize=ftell(in);
fseek(in,0,0);
if(fileSize%num==0) number=fileSize/num;
else number=fileSize/num+1;
buffer=(char*)malloc(BIT_SIZE);
filecount=fileSize/number;
sizecount=number/BIT_SIZE;
for(i=1;i<=filecount;i++,j=1)
{
sprintf(fileName,"%s%d.%s",firstInFileName,i,lastInFileName);
out=fopen(fileName,"wb");
while(j<=sizecount)
{
fread(buffer,BIT_SIZE,1,in);
fwrite(buffer,BIT_SIZE,1,out);
j++;
}
fread(buffer,number-BIT_SIZE*sizecount,1,in);
fwrite(buffer,number-BIT_SIZE*sizecount,1,out);
fclose(out);
}
if(fileSize-number*filecount-BIT_SIZE*sizecount!=0)
{
sprintf(fileName,"%s%d.%s",firstInFileName,i,lastInFileName);
out=fopen(fileName,"wb");
sizecount=(fileSize-number*filecount)/BIT_SIZE;
while(j<=sizecount)
{
fread(buffer,BIT_SIZE,1,in);
fwrite(buffer,BIT_SIZE,1,out);
j++;
}
fread(buffer,fileSize-number*filecount-BIT_SIZE*sizecount,1,in);
fwrite(buffer,fileSize-number*filecount-BIT_SIZE*sizecount,1,out);
fclose(out);
}
fclose(in);
free((void*)buffer);
free((void*)fileName);
printf("剪切成功!\n");
}
void combination(char firstInFileName[],char lastInFileName[],long number)//此函数可以合并大小不一的文件
{
char *buffer,*fileName;
long i=1,fileSize,j=1,n;
FILE *in,*out;
fileName=(char*)malloc(NAME_ESIZE);
buffer=(char*)malloc(BIT_SIZE);
if(number==0) number=NAME_ESIZE;
sprintf(fileName,"%s.%s",firstInFileName,lastInFileName);
out=fopen(fileName,"wb");
for(i=1;i<=number;i++,j=1)
{
sprintf(fileName,"%s%d.%s",firstInFileName,i,lastInFileName);
in=fopen(fileName,"rb");
if(in==NULL) {printf("找不到第%d个文件!\n",i);return;}
fseek(in,0,2);
fileSize=ftell(in);
fseek(in,0,0);
n=fileSize/BIT_SIZE;
while(j<=n)
{
fread(buffer,BIT_SIZE,1,in);
fwrite(buffer,BIT_SIZE,1,out);
j++;
}
fread(buffer,fileSize-n*BIT_SIZE,1,in);
fwrite(buffer,fileSize-n*BIT_SIZE,1,out);
fclose(in);
}
fclose(out);
free((void*)buffer);
free((void*)fileName);
printf("合并成功!\n");
}
[[it] 本帖最后由 faminxmu 于 2008-12-8 20:48 编辑 [/it]]
在虚拟的世界中寻找目标。