home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / text_cla / split.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-23  |  2.9 KB  |  166 lines

  1. /* split  will break up a file into n number of files argv[2] size bytes each */
  2.  
  3.  
  4. #include <dos.h>
  5.  
  6. #include <stdio.h>
  7.  
  8.  
  9.  
  10. char szChars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ$_";
  11.  
  12.  
  13. #define MAX_BUF 255
  14.  
  15.  
  16.  
  17. main(int argc,char * argv[])
  18. {
  19.  
  20.    int  iStat;
  21.    FILE * fpIn = NULL;
  22.    FILE * fpOut = NULL;
  23.    char  szBuffer[MAX_BUF + 1];
  24.    char  szInFileName[MAX_BUF + 1];
  25.    char  szOutFileName[MAX_BUF + 1];
  26.    int   iLen;
  27.    char * pc;
  28.    int   iFile;
  29.    long  lFileBytes;
  30.    long  lMaxBytes;
  31.    int   iNewFile;
  32.    int   iCharPos;
  33.  
  34.    
  35.    if(argc < 3)
  36.    {
  37.       printf("usage:  split  source_file  bytes_max_file\n");
  38.       return(1);
  39.    }
  40.  
  41.    
  42.    lMaxBytes = atol(argv[2]);
  43.  
  44.    printf("Splitting processing %s into files max %ld bytes each\n",
  45.             argv[1],lMaxBytes);
  46.  
  47.  
  48.  
  49.    strcpy(szInFileName,argv[1]);
  50.  
  51.    fpIn = fopen(szInFileName,"r");
  52.  
  53.    if(fpIn == NULL)
  54.    {
  55.       printf("Error Opening: %s\n", szInFileName);
  56.       return(1);
  57.    }
  58.  
  59.  
  60.  
  61.    /* set position in which the character will change = last character
  62.       in source filename */
  63.  
  64.    iCharPos = strlen(szInFileName)-1;
  65.  
  66.  
  67.    /* start with out file same as in file */
  68.  
  69.    strcpy(szOutFileName,szInFileName);
  70.  
  71.    
  72.    iFile = 0;
  73.    iNewFile = 1;
  74.  
  75.    while(!feof(fpIn))
  76.    {
  77.  
  78.       /* check for new dest file creation */
  79.  
  80.       if(iNewFile)
  81.       {
  82.          if(fpOut != NULL)
  83.          {
  84.             /* close last file */
  85.  
  86.             fclose(fpOut);
  87.             fpOut = NULL;
  88.             iFile++;  /* set for next file */
  89.          }
  90.  
  91.          /* reset filename */
  92.  
  93.          szOutFileName[iCharPos] = szChars[iFile];
  94.  
  95.  
  96.          /* check for naming conflict (regardless of case) */
  97.  
  98.          if(strcmpi(szOutFileName,szInFileName) == 0)
  99.          {
  100.             printf("Error Naming Conflict with next file.  Split stopped\n");
  101.             return(1);
  102.          }
  103.  
  104.  
  105.          /* open new/next out file */
  106.  
  107.          fpOut = fopen(szOutFileName,"w");
  108.  
  109.          if(fpOut == NULL)
  110.          {
  111.             printf("Error Opening: %s\n", szOutFileName);
  112.             fclose(fpIn);
  113.             return(1);
  114.          }
  115.  
  116.          printf("%s\n",szOutFileName);
  117.  
  118.  
  119.          /* reset bytes for this file, reset new file flag */
  120.          
  121.          lFileBytes = 0;
  122.  
  123.          iNewFile = 0;
  124.  
  125.       }
  126.  
  127.          
  128.       pc = fgets(szBuffer,MAX_BUF,fpIn);
  129.  
  130.       if(pc == NULL)
  131.          break;
  132.  
  133.       iLen = strlen(szBuffer);
  134.  
  135.       if(szBuffer[iLen -1] == '\n')
  136.          szBuffer[iLen -1] = '\0';
  137.  
  138.  
  139.       /* write text to output file */
  140.  
  141.       fprintf(fpOut,"%s\n",szBuffer);
  142.  
  143.  
  144.       /* track bytes into this file */
  145.  
  146.       lFileBytes += (long) strlen(szBuffer)+1;
  147.  
  148.  
  149.       /* if match/exceed number of bytes per file, mark for new file */
  150.  
  151.       if(lFileBytes >= lMaxBytes)
  152.          iNewFile = 1;
  153.  
  154.    }
  155.  
  156.    if(fpIn != NULL)
  157.       fclose(fpIn);
  158.  
  159.    if(fpOut != NULL)
  160.       fclose(fpOut);
  161.  
  162.  
  163.    return(0);
  164. }
  165.  
  166.