0

How can you read through a file of unknown line lengths (about 1500 lines, so no malloc/alloc and the like is needed because memory is sufficient...luckily, because I don't understand those array/pointer commands yet) including float numbers, signs, and letters, extract specific numbers from it, do some calculations and write them back in another file?

Three example-lines:

02060    6.1   0.15 K14C9 134.52612  339.34971  209.27800    6.93836  0.3820989  0.01956864  13.6383665  0 MPO319108  1304  45 1895-2014 0.53 M-v 38h MPCLINUX   000A   (2060) Chiron             20141030
05145    7.1   0.15 K14C9  90.96884  354.94362  119.25398   24.73205  0.5736395  0.01074547  20.3385073  0 MPO169571   319  21 1977-2009 0.58 M-v 38h MPCMEL     400A   (5145) Pholus             20090418
07066    9.6   0.15 K14C9  67.95075  170.25614   31.23622   15.65639  0.5195581  0.00813869  24.4774642  1 MPO135426   105   9 1993-2004 0.48 M-v 38h MPCW       400A   (7066) Nessus             20040526
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Lucas
  • 125
  • 8
  • `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); } fclose(f); fclose(g);` what is wrong? – Lucas Jan 11 '15 at 20:11
  • The values I need are exactly in the right order when comparing the code and example lines in the beginning question. I do see some of these values but there are mostly repetitions in the printed lines and also some arbitrary numbers. – Lucas Jan 11 '15 at 20:16

1 Answers1

0

Welcome! Including your existing code is always a good idea, so thanks for doing so. It would help to see it as part of the question, however. Could you edit the question to include your code, and format it nicely as well?

One problem that I see is with the correct use of formatting strings in your fscanf() call. You always specify %lf but that is only appropriate for double values, and you also need to parse integers and strings.

broadmonkey
  • 124
  • 2
  • 9