分割与合并文件程序源代码
分割文件程序
作者:佚名 来自:网络 人气:1000 加入时间:2006-1-22
/*** 文件分割 ***/
/***
运行程序时,如果编绎成的可执行文件名为fdiv.exe
则
若按字节数分割,命令行例如 fdiv d:\dire\file.nnn 2.5k
若按块数分割, 命令行例如 fdiv d:\dire\file.nnn (25)
分割结果:
例如 把file.nnn 分成 5 块 结果为file#nnn.001 --> file#nnn.005
! 注意:每次分割不得超过 999 块。而且如果超过 31 块,将不生成批拷贝文件
! 被分割的文件的文件名如果超过 4 个字符, 最好把它改为 <=4 (不算扩展名)
因为 DOS 下文件名只识别前 8 个字符,如 file --> file#nnn
***/
#define BYTE 0
#define PIECE 1 /*** 定义分割类型标识 ***/
#include
main(int argc,char **argv)
{
void fun (char *s); /*** 扩展名自加函数 ***/
FILE *fp_write,*fp_read,*fp_bat;
long num_in=0,byte_piece,pc_byte,total_byte;
int i,buffer=0,len,pc_fn=0,pc_float=0,byte_rest=0,sort_div;
char
*fn_in,fn_out[50],p[]=.000,fn_bat[50],fn_obj_bat[12],str_rest_byte[3],str_pc_fn[
3],ch;
switch (argc) /*** 命令行检测 ***/
{
case 3 : break;
case 2 :
printf (\n ERROR! you forgot to enter the number\n); exit (0);
case 1 :
printf (\n ERROR! you forgot to enter the file name and the number\n);
exit (0);
}
fn_in=argv[1];
if ((fp_read=fopen(fn_in,rb))==NULL) /*** 打开被分割的文件 ***/
{ printf (\n ERROR! the file not exists\n); exit (0); }
fseek (fp_read,0L,2); total_byte=ftell (fp_read); rewind (fp_read);
if (*argv[2]=='\(') /*** 检测分割类型 ***/
{
sort_div=PIECE;
i=1;
while (argv[2][i]>='0'&&argv[2][i]<='9')
num_in=num_in*10+argv[2][i++]-'0'; /*** n 为输入的块数 ***/
if (argv[2][i]!='\)'&&argv[2][i])
{ printf (\n ERROR! the entered NUMBER is invalid\n); exit (0); }
if (num_in>999)
{ printf (\n ERROR! can not creat more than 999 files\n); exit (0); }
byte_rest=total_byte%num_in; byte_piece=total_byte/num_in; if (byte_rest>0) byte_piece++;
}
else
{
sort_div=BYTE;
i=0;
while (argv[2][i]>='0'&&argv[2][i]<='9') { num_in=num_in*10+argv[2][i]-'0'; i++; }
if (argv[2][i]=='.')
{
i++; while (argv[2][i]>='0'&&argv[2][i]<='9')
{ num_in=num_in*10+argv[2][i]-'0'; i++; pc_float++; }
}
ch=argv[2][i];
if (ch=='K'||ch=='k') num_in*=1024; if (ch=='M'||ch=='m') num_in=num_in*1024*1024;
if (ch&&ch!='.'&&ch!='k'&&ch!='K'&&ch!='m'&&ch!='M')
{ printf (\n ERROR! the entered NUMBER is invalid\n); exit (0); }
for (i=1;i<=pc_float;i++) num_in=num_in/10;
byte_piece=num_in;
if (total_byte%byte_piece==0) pc_fn=total_byte/byte_piece; else pc_fn=total_byte/byte_piece+1;
if (pc_fn>999)
{ printf (\n ERROR! can not creat more than 999 files\n); exit (0); }
}
i=0; /*** d:\dire\file.nnn --->d:\dire\file#nnn ***/
while (*(fn_in+i))
{
if (*(fn_in+i)!='.') *(fn_out+i)=*(fn_in+i); else *(fn_out+i)='#'; i++;
}
*(fn_out+i)='\0';
strcpy (fn_bat,fn_out); strcpy(fn_obj_bat,fn_out); strcat (fn_bat,.bat);
/*** d:\dire\file#nnn --> d:\dire\file#nnn.bat ***/
strcat (fn_out,p); len=strlen (fn_out);
/*** d:\dire\file#nnn --->dire#nnn.000 ***/
/*** 分割 ***/
fread (&buffer,1,1,fp_read); pc_fn=0;
while (!feof(fp_read))
{
fun (fn_out+len-3); /*** 调用扩展名自加函数 ***/
fp_write=fopen(fn_out,wb);
pc_byte=0;
while (!feof(fp_read))
{
fwrite (&buffer,1,1,fp_write); ++pc_byte; fread (&buffer,1,1,fp_read);
if (pc_byte==byte_piece) { fclose (fp_write); break; }
}
pc_fn++;
if (sort_div==PIECE) if (pc_fn==byte_rest) byte_piece--;
/*** 若按块分割,前 byte_rest 个块文件均比后面的块文件多 1 个字节 ***/
}
fclose (fp_read); fclose (fp_write);
/**** 生成拷贝文件 ***/
if (pc_fn<=31)
{
fp_bat=fopen(fn_bat,w); fputs (copy /b ,fp_bat);
len=strlen (fn_obj_bat);
while (fn_obj_bat[len-1]!='\\'&&len-1>=0) len--; i=0;
while (fn_obj_bat[len+i]) { fn_obj_bat[i]=fn_obj_bat[len+i]; i++; } fn_obj_bat[i]='\0';
/*** 分离出目标文件名,(取出 fn_in 的关键词) ***/
strcat (fn_obj_bat,.000);
len=strlen(fn_obj_bat);
for (i=1;i<=pc_fn;i++)
{
fun (fn_obj_bat+len-3); /*** 调用扩展名自加函数 ***/
if (i>1) fputs ( + ,fp_bat);
fputs (fn_obj_bat,fp_bat);
}
len=strlen (fn_in);
while (fn_in[len-1]!='\\'&&len-1>=0) len--; i=0;
while (fn_in[len+i]) { fn_in[i]=fn_in[len+i]; i++; } fn_in[i]='\0';
fputs ( ,fp_bat); fputs (fn_in,fp_bat);
fclose (fp_bat);
}
/***
报告
软件系统测试报告下载sgs报告如何下载关于路面塌陷情况报告535n,sgs报告怎么下载竣工报告下载
分割结果 ***/
printf (\n OK! %d files have been created,pc_fn);
if (pc_fn<=31) printf ( AND the batch file\n);
else printf ( NO the batch file\n);
if (byte_rest>0)byte_piece++;
if (byte_piece==pc_byte) { printf (\n ALL files are %ld B\n,byte_piece); exit
(0); }
str_pc_fn[0]=pc_fn/100+'0'; str_pc_fn[1]=pc_fn/10%10+'0'; str_pc_fn[2]=pc_fn%10+'0'; str_pc_fn[3]='\0';
switch (sort_div)
{
case PIECE :
if (byte_rest)
{
str_rest_byte[0]=byte_rest/100+'0';
str_rest_byte[1]=byte_rest/10%10+'0';
str_rest_byte[2]=byte_rest%10+'0'; str_rest_byte[3]='\0';
}
if (byte_rest==1) printf (\n NO.001 is %ld B\n,byte_piece);
if (byte_rest>1) printf (\n NO.001 --> NO.%s are %ld B\n,str_rest_byte,byte_piece);
if (pc_fn-byte_rest==1) printf (\n NO.%s is %ld B\n,str_pc_fn,pc_byte);
if (pc_fn-byte_rest>1)
{ fun (str_rest_byte); printf (\n NO.%s --> NO.%s are %ld B\n,str_rest_byte,str_pc_fn,pc_byte); }
break;
case BYTE :
if (pc_fn==2) printf (\n NO.001 is %ld B\n,byte_piece);
else
{
/*** 借用 str_rest_byte 存储 pc_fn-- ***/
pc_fn--;
str_rest_byte[0]=pc_fn/100+'0'; str_rest_byte[1]=pc_fn/10%10+'0';
str_rest_byte[2]=pc_fn%10+'0';
printf (\n NO.001 --> NO.%s are %ld B\n,str_rest_byte,byte_piece);
}
printf (\n NO.%s is %ld B\n,str_pc_fn,pc_byte);
}
}
/*** 定义扩展名自加函数 ***/
void fun (char *s)
{
int i;
if (s[2]<'9') s[2]++;
else if (s[1]<'9') { s[2]-=9; s[1]++; }
else { s[2]-=9; s[1]-=9; s[0]++; }
}
合并文件程序
作者:佚名 来自:网络 人气:1000 加入时间:2006-1-22
/*** 文件合并 ***/
/***
运行程序时,如果编绎成的可执行文件名为fadd.exe
则 命令行:例如 fadd file#nnn
主意:输入 file#nnn ,而不是 file#nnn.001 ***/
/*** 当分快文件少于 32 块时,可以不用此程序,
直接运行分割时生成的披拷贝文件即可 ***/
#include
main(int argc,char **argv)
{
FILE *fp_read,*fp_write;
int buffer=0,i,len,pc_fn=1;
char *fn_in,fn_out[50],*p=.001,ch='#';
if (argc==1)
{
printf (\n ERROR! you fogot enter the file name ( file#nnn )\n);
exit (0);
}
fn_in=argv[1];
i=0; /*** file#nnn ---> file.nnn 得到目标文件名 ***/
while (*(fn_in+i))
{
if (*(fn_in+i)=='#') *(fn_out+i)='.'; else *(fn_out+i)=*(fn_in+i); i++;
}
*(fn_out+i)='\0';
strcat (fn_in,p); len=strlen (fn_in); /*** file#nnn ---> file#nnn.01 ***/
if ((fp_read=fopen (fn_in,rb))==NULL) /*** 检测块文件是否存在***/
{ printf (\n ERROR! the file \ %s \ not exsits\n,fn_in); exit (0); }
/*** 检测目标文件是否已经存在。如果存在,交出控制权 ***/
if ((fp_write=fopen(fn_out,rb))!=NULL)
{
fclose (fp_write);
printf (\n OVERWRITE the file \ %s \ (Y/N)?,fn_out);
while (ch!='N'&&ch!='n'&&ch!='Y'&&ch!='y')
{
ch=getche();
if (ch=='Y'||ch=='y') break;
if (ch=='N'||ch=='n') { printf (\n); exit (0); }
}
printf (\n);
}
/*** 合并 ***/ /*** 第一个块文件已在前面检测时打开 ***/
fp_write=fopen(fn_out,wb); /*** 建立目标文件 ***/
while (fp_read) /*** 块文件不能被打开 (不存在 ) 时,停下 ***/
{
fread (&buffer,1,1,fp_read);
while (!feof(fp_read))
{
fwrite (&buffer,1,1,fp_write); fread (&buffer,1,1,fp_read);
}
fclose (fp_read);
pc_fn++;
if (pc_fn%10==0&&pc_fn%100!=0)
{ *(fn_in+len-2)+=1; *(fn_in+len-1)-=9; }
else if (pc_fn%100!=0) *(fn_in+len-1)+=1;
if (pc_fn%100==0) { *(fn_in+len-3)+=1; *(fn_in+len-2)-=9; *(fn_in+len-1)-=9; }
fp_read=fopen(fn_in,rb);
}
fclose (fp_read); fclose (fp_write);
printf (\n OK! the file \ %s \ be created successfuly\n,fn_out);
printf (\n %d files been added\n,pc_fn-1);
}