home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / SPLIT.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  68 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  SPLIT.C - A utility to split large text files into smaller files
  5. **
  6. **  public domain by Bob Stout
  7. **
  8. **  uses FNSPLIT.C from SNIPPETS
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "extkword.h"
  15. #include "filnames.h"
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19.       long newsize = 32L * 1024L;
  20.       size_t seq = 0;
  21.       char fname[FILENAME_MAX];
  22.       FILE *from;
  23.  
  24.       if (2 > argc)
  25.       {
  26.             puts("SPLIT big_file [size_in_K]\n");
  27.             puts("creates files of the same name, "
  28.                   "but with numeric extensions");
  29.             puts("a maximum file size may be specified for new files");
  30.             return EXIT_SUCCESS;
  31.       }
  32.       if (2 < argc)
  33.       {
  34.             newsize   = atol(argv[2]);
  35.             newsize <<= 10;
  36.       }
  37.       if (NULL == (from = fopen(argv[1], "r")))
  38.       {
  39.             printf("\aSPLIT: error - can't open %s\n", argv[1]);
  40.             return EXIT_FAILURE;
  41.       }
  42.       fnSplit(argv[1], NULL, NULL, NULL, NULL, fname, NULL);
  43.       while (!feof(from))
  44.       {
  45.             char newname[FILENAME_MAX], buf[1024];
  46.             FILE *to;
  47.             long bytes;
  48.  
  49.             sprintf(newname, "%s.%03d", fname, seq++);
  50.             if (NULL == (to = fopen(newname, "w")))
  51.             {
  52.                   printf("\aSPLIT: error - can't write %s\n", newname);
  53.                   return EXIT_FAILURE;
  54.             }
  55.             for (bytes = 0L; !feof(from) && (bytes < newsize); )
  56.             {
  57.                   if (fgets(buf, 1023, from))
  58.                   {
  59.                         fputs(buf, to);
  60.                         bytes += (long)strlen(buf);
  61.                   }
  62.             }
  63.             fclose(to);
  64.             printf("%s written\n", newname);
  65.       }
  66.       return EXIT_SUCCESS;
  67. }
  68.