home *** CD-ROM | disk | FTP | other *** search
- /* split will break up a file into n number of files argv[2] size bytes each */
-
-
- #include <dos.h>
-
- #include <stdio.h>
-
-
-
- char szChars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ$_";
-
-
- #define MAX_BUF 255
-
-
-
- main(int argc,char * argv[])
- {
-
- int iStat;
- FILE * fpIn = NULL;
- FILE * fpOut = NULL;
- char szBuffer[MAX_BUF + 1];
- char szInFileName[MAX_BUF + 1];
- char szOutFileName[MAX_BUF + 1];
- int iLen;
- char * pc;
- int iFile;
- long lFileBytes;
- long lMaxBytes;
- int iNewFile;
- int iCharPos;
-
-
- if(argc < 3)
- {
- printf("usage: split source_file bytes_max_file\n");
- return(1);
- }
-
-
- lMaxBytes = atol(argv[2]);
-
- printf("Splitting processing %s into files max %ld bytes each\n",
- argv[1],lMaxBytes);
-
-
-
- strcpy(szInFileName,argv[1]);
-
- fpIn = fopen(szInFileName,"r");
-
- if(fpIn == NULL)
- {
- printf("Error Opening: %s\n", szInFileName);
- return(1);
- }
-
-
-
- /* set position in which the character will change = last character
- in source filename */
-
- iCharPos = strlen(szInFileName)-1;
-
-
- /* start with out file same as in file */
-
- strcpy(szOutFileName,szInFileName);
-
-
- iFile = 0;
- iNewFile = 1;
-
- while(!feof(fpIn))
- {
-
- /* check for new dest file creation */
-
- if(iNewFile)
- {
- if(fpOut != NULL)
- {
- /* close last file */
-
- fclose(fpOut);
- fpOut = NULL;
- iFile++; /* set for next file */
- }
-
- /* reset filename */
-
- szOutFileName[iCharPos] = szChars[iFile];
-
-
- /* check for naming conflict (regardless of case) */
-
- if(strcmpi(szOutFileName,szInFileName) == 0)
- {
- printf("Error Naming Conflict with next file. Split stopped\n");
- return(1);
- }
-
-
- /* open new/next out file */
-
- fpOut = fopen(szOutFileName,"w");
-
- if(fpOut == NULL)
- {
- printf("Error Opening: %s\n", szOutFileName);
- fclose(fpIn);
- return(1);
- }
-
- printf("%s\n",szOutFileName);
-
-
- /* reset bytes for this file, reset new file flag */
-
- lFileBytes = 0;
-
- iNewFile = 0;
-
- }
-
-
- pc = fgets(szBuffer,MAX_BUF,fpIn);
-
- if(pc == NULL)
- break;
-
- iLen = strlen(szBuffer);
-
- if(szBuffer[iLen -1] == '\n')
- szBuffer[iLen -1] = '\0';
-
-
- /* write text to output file */
-
- fprintf(fpOut,"%s\n",szBuffer);
-
-
- /* track bytes into this file */
-
- lFileBytes += (long) strlen(szBuffer)+1;
-
-
- /* if match/exceed number of bytes per file, mark for new file */
-
- if(lFileBytes >= lMaxBytes)
- iNewFile = 1;
-
- }
-
- if(fpIn != NULL)
- fclose(fpIn);
-
- if(fpOut != NULL)
- fclose(fpOut);
-
-
- return(0);
- }
-
-