/* Split the lines of the input files, in a sequential way, between up to 10 output files. The supplied file name should contain the list of files to merge and split, one file name per line */ #include #include #include #include int main(int argc, char *argv[]) { char fullLine[1024], fn[6][1024]; int totalLines; int outputLines; int splitWay; char *cPnt; FILE *fList; FILE *f1,*f2,*f3,*f4,*f5,*f6, *o1,*o2,*o3,*o4,*o5,*o6,*o7,*o8,*o9,*o10; int red1, red2, red3, red4, red5, red6, rIn; int i,j,fin; if( argc != 3 ) { printf ("%d is the wrong number of arguments\n", argc-1); printf("Split the lines of the input files, in a sequential way,\n"); printf("between up to 10 output files.\n\n"); printf("The supplied file name should contain the list of files to\n"); printf("merge and split (up to 6), one file name per line\n"); printf (" Please supply: input_file_name AND split-way (from 2-10)\n"); exit(1); } splitWay = atoi(argv[2]); if ( splitWay < 2 || splitWay > 10 ) { printf ("%d-way split is not legal\n", splitWay); exit(1); } fList = fopen(argv[1], "r"); rIn = 0; while(1) { cPnt = fgets( fullLine, 1024, fList ); if (cPnt == NULL ) break; strcpy((char *) &(fn[rIn][0]), cPnt); fn[rIn][strlen(cPnt)-1] = '\0'; if( ++rIn > 5 ) break; } fclose(fList); if( rIn == 0 ) { printf ("No input file names read, abort\n"); exit(1); } totalLines=0; outputLines=0; red1=0; red2=1; red3=1; red4=1; red5=1; red6=1; switch (rIn) { case 6: red6=0; f6 = fopen((char *)&(fn[5][0]), "r"); case 5: red5=0; f5 = fopen((char *)&(fn[4][0]), "r"); case 4: red4=0; f4 = fopen((char *)&(fn[3][0]), "r"); case 3: red3=0; f3 = fopen((char *)&(fn[2][0]), "r"); case 2: red2=0; f2 = fopen((char *)&(fn[1][0]), "r"); case 1: red1=0; f1 = fopen((char *)&(fn[0][0]), "r"); break; } i=1; j=1; fin=0; switch ( splitWay ) { case 10: o10 = fopen("mrg10.txt", "w"); case 9: o9 = fopen("mrg09.txt", "w"); case 8: o8 = fopen("mrg08.txt", "w"); case 7: o7 = fopen("mrg07.txt", "w"); case 6: o6 = fopen("mrg06.txt", "w"); case 5: o5 = fopen("mrg05.txt", "w"); case 4: o4 = fopen("mrg04.txt", "w"); case 3: o3 = fopen("mrg03.txt", "w"); case 2: o2 = fopen("mrg02.txt", "w"); o1 = fopen("mrg01.txt", "w"); break; default: printf ("%d-way split is not legal\n", splitWay); exit(1); break; } while(1) { fin=0; if ( red1+red2+red3+red4+red5+red6 == 6 ) { break; } switch (i) { case 1: if (red1 == 0 ) { cPnt = fgets( fullLine, 512, f1 ); if ( cPnt == NULL ) { red1=1; fin=1; } } else { fin=1; } break; case 2: if (red2 == 0 ) { cPnt = fgets( fullLine, 512, f2 ); if ( cPnt == NULL ) { red2=1; fin=1; } } else { fin=1; } break; case 3: if (red3 == 0 ) { cPnt = fgets( fullLine, 512, f3 ); if ( cPnt == NULL ) { red3=1; fin=1; } } else { fin=1; } break; case 4: if (red4 == 0 ) { cPnt = fgets( fullLine, 512, f4 ); if ( cPnt == NULL ) { red4=1; fin=1; } } else { fin=1; } break; case 5: if (red5 == 0 ) { cPnt = fgets( fullLine, 512, f5 ); if ( cPnt == NULL ) { red5=1; fin=1; } } else { fin=1; } break; case 6: if (red6 == 0 ) { cPnt = fgets( fullLine, 512, f6 ); if ( cPnt == NULL ) { red6=1; fin=1; } } else { fin=1; } break; default: fin=1; break; } i=(i<6)?i+1:1; if ( fin == 1 ) { continue; } totalLines++; // pLast = cPnt + strlen(fullLine); switch (j) { case 1: fputs(fullLine, o1); break; case 2: fputs(fullLine, o2); break; case 3: fputs(fullLine, o3); break; case 4: fputs(fullLine, o4); break; case 5: fputs(fullLine, o5); break; case 6: fputs(fullLine, o6); break; case 7: fputs(fullLine, o7); break; case 8: fputs(fullLine, o8); break; case 9: fputs(fullLine, o9); break; case 10: fputs(fullLine, o10); break; default: break; } j=(j