/* Extract the in initial, final and losses from an echod file that Dimad generates and writes them to separate files with names initial00.dat, final00.dat, and losses00.dat. If the echod file contains data from more than one run several output files with incremented suffix are produced. Compile with: gcc echo2files.c -o echo2files V. Ziemann, TSL, 050529 */ #include #include int main(int argc, char *argv[]) { char line[256]; int iset=-1; FILE *fp; while (fgets(line,255,stdin)) { if (strstr(line,"INITIAL POSITIONS OF PARTICLES")) { iset++; sprintf(line,"initial%02d.dat",iset); printf("%s\n",line); fp=fopen(line,"w"); if (fp==NULL) {puts("Cannot open initial file"); exit(1);} fgets(line,255,stdin); // empty line at start while (fgets(line,255,stdin)) { if (strlen(line) < 10) break; // empty line at end fprintf(fp,"%s",line); } fclose(fp); fp=NULL; } if (strstr(line,"PARTICLE POSITIONS AFTER ELEMENT")) { sprintf(line,"final%02d.dat",iset); printf("%s\n",line); fp=fopen(line,"w"); if (fp==NULL) {puts("Cannot open final file"); exit(1);} fgets(line,255,stdin); // empty line at start while (fgets(line,255,stdin)) { if (strlen(line) < 10) break; // empty line at end fprintf(fp,"%s",line); } fclose(fp); fp=NULL; } if (strstr(line," LOST PARTICLE DATA:")) { sprintf(line,"losses%02d.dat",iset); printf("%s\n",line); fp=fopen(line,"w"); if (fp==NULL) {puts("Cannot open losses file"); exit(1);} fgets(line,255,stdin); // empty line at start while (fgets(line,255,stdin)) { if (strlen(line) < 10) break; // empty line at end fprintf(fp,"%s",(line+21)); // drop the first 21 characters } fclose(fp); fp=NULL; } } return 0; }