#include #include int main(int argc, char* argv[]) /* program to split 4-column file when slope of first column changes sign */ /* writes files .0, .1, etc. */ { FILE* inFile; FILE* outFile; int nFile, nRead, goodLines, i; double c1, c2, c3, c4, c5; double p1, p2, p3, p4; double pp1, pp2, pp3, pp4; double dum, pp, cp; char outName[100]; char line[100]; /* check command line argument count */ if (argc != 2) { printf("Split [filename]\n"); exit(1); } /* open input file */ inFile=fopen(argv[1],"r"); /* check for open error */ if (inFile == NULL) { printf("Error opening input file='%s'\n", argv[1]); exit(1); } printf("Opened input file '%s'\n", argv[1]); /* name of first output file */ nFile=0; sprintf(outName,"%s.%i", argv[1], nFile); /* open first output file */ outFile=fopen(outName,"w"); /* check for open error */ if (outFile == NULL) { printf("Error opening output file='%s'\n", outName); exit(1); } /* init good lines */ goodLines = 0; /* loop over lines */ for (i=1; i<10000; i++) { /* read line of input file into string array */ fgets(line, 99, inFile); /* check for end of file */ if (feof(inFile)) { printf("end of input file at line=%i\n", i); break; } /* skip line if first character is # */ if (line[0] == '#') { printf("%3i: %s", i, line); printf("line=%i skipped\n", i); continue; } /* read string to count variables */ nRead=sscanf(line,"%lf %lf %lf %lf %lf", &dum, &dum, &dum, &dum, &dum); /* check how many were read */ if (nRead <= 0) { printf("%3i: %s", i, line); printf("line=%i skipped\n", i); continue; } if (nRead != 4) { printf("%3i: %s", i, line); printf("Error parsing line=%i, expected 4 numbers, found %i\n", i, nRead); printf("error exit\n"); exit(1); } /* increment good line counter */ goodLines++; /* shift previous values */ pp1 = p1; pp2 = p2; pp3 = p3; pp4 = p4; p1 = c1; p2 = c2; p3 = c3; p4 = c4; /* read string into character variables */ nRead=sscanf(line,"%lf %lf %lf %lf %lf", &c1, &c2, &c3, &c4, &c5); /* check that buffers are up to date */ if (goodLines > 2) { /* write the data from 2 lines ago */ fprintf(outFile,"%f \t %f \t %f \t %f\n", pp1, pp2, pp3, pp4); /* calculate differences */ cp = c1 - p1; pp = p1 - pp1; /* check if slope changed sign */ if ((pp > 0 && cp <= 0) || (pp < 0 && cp >= 0) ) { /* close present file */ fclose(outFile); printf("line=%i, closed output file '%s'\n", i, outName); /* name of next output file */ nFile++; sprintf(outName,"%s.%i", argv[1], nFile); /* open next output file */ outFile=fopen(outName,"w"); /* check for open error */ if (outFile == NULL) { printf("Error opening output file'%s'\n", outName); exit(1); } /* arrange to skip ambiguous data */ goodLines = 1; } } } /* close present file */ fclose(outFile); printf("line=%i, closed output file '%s'\n", i, outName); return 0; }