/* Split the lines of the input file, in a sequential way, between up to 10 output files. This is a stripped down version of multi_split_lists.c, where multiple input files are provided. */ #include #include #include #include int main(int argc, char *argv[]) { char fullLine[1024]; int totalLines; int outputLines; int splitWay; char *cPnt; FILE *f1,*f2,*f3,*f4,*f5,*f6, *o1,*o2,*o3,*o4,*o5,*o6,*o7,*o8,*o9,*o10; int red1, red2, red3, red4, red5, red6; 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 file, in a sequential way,\n"); printf ("between up to 10 output files.\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); } totalLines=0; outputLines=0; red1=0; red2=1; red3=1; red4=1; red5=1; red6=1; i=1; j=1; fin=0; f1 = fopen(argv[1], "r"); 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