Try this:
Code:
#include <stdlib.h>
#include <stdio.h>
int main (void) {
int i, j;
double x, y, z;
char line[4096];
FILE *fp, *fp2;
fp = fopen ("abcd10.txt", "r");
fp2 = fopen ("xyz.gro", "w");
for (j=0; j<10; j++) {
fgets(line, sizeof line, fp);
fgets(line, sizeof line, fp);
for (i = 0; i < 13968; i++) {
fgets(line, sizeof line, fp);
if (sscanf(line, "%*f %*f %lf %lf %lf", &x, &y, &z) != 3) {
printf("bad line: j=%d i=%d\n[%s]\n", j, i, line);
return EXIT_FAILURE;
}
if (i % 4 == 3)
fprintf(fp2, "%.5f %.5f %.5f\n", x, y, z);
}
fgets(line, sizeof line, fp);
}
fclose(fp);
fclose(fp2);
return 0;
}
I changed it to first read in the line with fgets and then scan the line with sscanf, which I should have done in the first place since that's basically always the proper thing to do.
Now that I think about it, it was the combination of fgets and fscanf that screwed it up. The fscanf leaves the newline at the end of the line, so the next fgets only reads that and you end up falling a line behind. Dumb mistake on my part. So it wasn't actually printing i somehow, but instead it was printing the second column as x (the one that's counting upwards) and in fact your y and z would've been incorrect too since they would have the values that should have been in x and y, respectively.