• 当前位置:首页>>C语言>>C语言编程实例>>分割文件程序
  • 分割文件程序
  •  /*** 文件分割 ***/

     /***
                   运行程序时,如果编绎成的可执行文件名为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<stdio.h>
     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); }
        }

    [1] [2] [3] [4] 下一页