0

I have a following simple code:

f = fopen("tdat.dat","r");

for(int i=0;;i++)
{
    if(feof(f)) break;
    fscanf(f,"%lf%lf%lf%lf",&x,&y,&z,&t);
    printf("%lf\t%lf\t%lf\t%lf\n",x,y,z,t);
}

and my datafile tdat.dat is as follows:

1.2 0.8 2.1 30.0
1.5 2.3 4.5 15.0
0.3 1.6 1.5 21.0

When I run the program, the result is:

1.200000    0.800000    2.100000    30.000000
1.500000    2.300000    4.500000    15.000000
0.300000    1.600000    1.500000    21.000000
0.300000    1.600000    1.500000    21.000000

My question is: why is there an additional line in the result that is same as the previous line? (This problem also appears when I use "while()" loop instead of for() loop)

Also, how can I avoid this problem?

Lê Dũng
  • 113
  • 5

0 Answers0