C   30

filesplitter

Guest on 24th June 2022 02:47:31 PM

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. void usage(char *progname)
  5. {
  6.   printf("Usage: %s <input file name> <output root name>\n"
  7.          "where the output files will be named\n"
  8.          "<output root name>.1 ,<output root name>.2, etc\n",
  9.          progname );
  10.   exit(1);
  11. }
  12.  
  13.  
  14. int main(int argc, char * argv[])
  15. {
  16.   const long int flopsize = 1430000;
  17.   long int noutchars;
  18.   char *poutname;
  19.   int noutfile = 0;
  20.  
  21.   int inchar;
  22.   FILE *fpin, *fpout;
  23.  
  24.   if( argc != 3 ) usage(argv[0]);
  25.  
  26.   fpin = fopen(argv[1],"r");
  27.   if( fpin == NULL )
  28.     {
  29.       perror("Cannot open input file.\n");
  30.       usage(argv[0]);
  31.     }
  32.  
  33.   poutname = (char *) malloc( strlen(argv[2])+12 );
  34.   if( poutname == NULL )
  35.     { perror("Bad outfile name?\n");
  36.       usage(argv[0]);
  37.     }
  38.  
  39.   inchar = getc(fpin);
  40.   noutchars = 0;
  41.  
  42.   while( inchar != EOF )
  43.     {
  44.       if( noutchars >= flopsize )
  45.         {
  46.           fclose(fpout);
  47.           noutchars = 0;
  48.         }
  49.  
  50.       if( noutchars == 0 )
  51.         {
  52.           sprintf(poutname,"%s.%d", argv[2], ++noutfile);
  53.           printf("Writing file %s ...\n", poutname);
  54.           fpout = fopen(poutname,"w");
  55.         }
  56.  
  57.       putc((char) inchar, fpout);
  58.       noutchars++;
  59.       inchar = getc(fpin);
  60.     }
  61.  
  62.   if(noutchars == 0)
  63.     fclose(fpout);
  64.  
  65.   fclose(fpin);
  66.  
  67.   return(0);
  68. }

Raw Paste


Login or Register to edit or fork this paste. It's free.