0

I have the following code and the compiler doesn't show me any error or warning messages. But although the conditions are fulfilled it doesn't do what I want.

#include <stdio.h>

int main()
{
    FILE *f,*g;
    f = fopen("MPCORB_Distant.txt","r");
    g = fopen("MPCORB_Distant_AvgKBOValues.txt","w");
    float Nbr,H,G,Epoch,M,w,W,i,e,n,a,UP,Ref,Obs,Opp,Arc,rms,Pert1,Pert2,
        Comp,Type,Name,LastObs;
    float Res_a,Res_e,Res_i,Res_W,Res_w;

    while(!feof(f))
    {
        fscanf(f,"%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\n",
            &Nbr,&H,&G,&Epoch,&M,&w,&W,&i,&e,&n,&a,&UP,&Ref,&Obs,&Opp,&Arc,&rms,&Pert1,
            &Pert2,&Comp,&Type,&Name,&LastObs);

        if ( a > 60 )
        {
            fprintf(g,"%f %f %f %f %f\n",a,e,i,W,w);
        }
        else
        {
        }
    }

    fclose(f);
    fclose(g);

    return 0;
}

When I write a printf("Hello\n"); inside the else condition it doesn't stop writing. It seems not to enter the if-condition at all, despite the condition being fulfilled quite soon in the list of the file that is read.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Lucas
  • 125
  • 8

1 Answers1

0

I didn't test it but I think the problem is fscanf not reading the line properly. You should check the return value of fscanf for EOF/Conversion error instead of using feof and relying on the success of the read operation.

This question here: Using fscanf() using feof() seems to describe the same problem and some details on feof are given in the first answer to this question: Why is “while ( !feof (file) )” always wrong? .

Community
  • 1
  • 1
frow
  • 874
  • 8
  • 13
  • I will read through it. But we learned it this way and it always worked as I wanted – Lucas Jan 10 '15 at 13:31
  • What about: while(fscanf(f,"%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\n",&Nbr,&H,&G,&Epoch,&M,&w,&W,&i,&e,&n,&a,&UP,&Ref,&Obs,&Opp,&Arc,&rms,&Pert1,&Pert2,&Comp,&Type,&Name,&LastObs) != EOF) { printf("%f %f %f %f %f\n",a,e,i,W,w); } In the .txt file there are not only numbers but also letters. might this be a problem as well? – Lucas Jan 11 '15 at 19:02
  • Yes, I think so - if fscanf can't parse a certain line it will leave it unparsed. You could read the line (using fgets for example) and then parse it manually using sscanf. If the line can not be parsed, then the program will still continue as fgets will continue to read your file. – frow Jan 12 '15 at 11:05
  • `FILE *f,*g; f = fopen("MPCORB_Distant.txt","r"); g = fopen("MPCORB_Distant_AvgKBOValues.txt","w"); double Nbr,H,G,Epoch,M,w,W,i,e,n,a,UP,Ref,Obs,Opp,Arc,rms,Pert1,Pert2,Comp,Type,Name,LastObs; double Res_a,Res_e,Res_i,Res_W,Res_w; int c; while ((c = fgetc(f)) != EOF) { fscanf(f,"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf\n",&Nbr,&H,&G,&Epoch,&M,&w,&W,&i,&e,&n,&a,&UP,&Ref,&Obs,&Opp,&Arc,&rms,&Pert1,&Pert2,&Comp,&Type,&Name,&LastObs); fprintf(g,"%.8lf %.8lf %.8lf %.8lf %.8lf\n",w,W,i,e,a); }` is better. But can you give me a minimal example? – Lucas Jan 12 '15 at 16:51