From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/split.c | 71 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/split.c (limited to 'reference/C/CONTRIB/SNIP/split.c') diff --git a/reference/C/CONTRIB/SNIP/split.c b/reference/C/CONTRIB/SNIP/split.c new file mode 100755 index 0000000..cd3b978 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/split.c @@ -0,0 +1,71 @@ +/* +** SPLIT.C - A utility to split large text files into smaller files +** +** public domain by Bob Stout +** +** uses PSPLIT.C from SNIPPETS +*/ + +#include +#include +#include + +#ifdef __TURBOC__ + #define FAR far +#else + #define FAR _far +#endif + +void psplit(char *, char *, char *, char *, char *); + +int main(int argc, char *argv[]) +{ + long newsize = 32L * 1024L; + size_t seq = 0; + char fname[FILENAME_MAX]; + FILE *from; + + if (2 > argc) + { + puts("SPLIT big_file [size_in_K]\n"); + puts("creates files of the same name, " + "but with numeric extensions"); + puts("a maximum file size may be specified for new files"); + return EXIT_SUCCESS; + } + if (2 < argc) + { + newsize = atol(argv[2]); + newsize <<= 10; + } + if (NULL == (from = fopen(argv[1], "r"))) + { + printf("\aSPLIT: error - can't open %s\n", argv[1]); + return EXIT_FAILURE; + } + psplit(argv[1], NULL, NULL, fname, NULL); + while (!feof(from)) + { + char newname[FILENAME_MAX], buf[1024]; + FILE *to; + long bytes; + + sprintf(newname, "%s.%03d", fname, seq++); + if (NULL == (to = fopen(newname, "w"))) + { + printf("\aSPLIT: error - can't write %s\n", newname); + return EXIT_FAILURE; + } + for (bytes = 0L; !feof(from) && (bytes < newsize); ) + { + if (fgets(buf, 1023, from)) + { + fputs(buf, to); + bytes += (long)strlen(buf); + } + } + fclose(to); + printf("%s written\n", newname); + } + return EXIT_SUCCESS; +} -- cgit v1.2.3-54-g00ecf