Re: [AMBER] sscanf again

From: Chris Moth <chris.moth.Vanderbilt.Edu>
Date: Wed, 27 Jan 2010 13:25:19 -0600 (CST)

You need to read more than one line from the file. You need to read all
of them.

Add line=getline(pf); inside the loop.

Otherwise, sscanf reads the same string over and over.

Now you have made progress, perhaps you should try fscanf and avoid the
intermediate string.

These questions you are asking now are very basic computer programming
questions. I recommend you find a programmer (you don't need a chemist)
at your institution who can help you through this more quickly.

C

> Yes, sscanf (line, "lf",f) works well. My new question here is that I
> want
> it to read the data into a point array, but it only reads the first three
> numbers from the string in my testing program (code below):
>
> //////////////////////////////////////////
> //testing
> string line, s;
> point a[4];
> int n;
> file pf;
> float b;
>
>
> pf=fopen("data.txt","r");
> line=getline(pf);
> printf("%s\n",line);
> for(n=0;n<4;n=n+1) {
> sscanf(line,"%lf %lf %lf",a[n].x, a[n].y, a[n].z);
> printf ("%f %f %f",a[n].x,a[n].y,a[n].z);
> printf ("\t n=%d,done!\n", n);
> }
>
> fclose(pf);
> /////////////////////////////////////////////
> The data in my data.txt is:
> 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0
>
> this is also the value of 'line'. I got the following:
>
> ./a.out
> 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0
> 1.000000 2.000000 3.000000 n=0,done!
> 1.000000 2.000000 3.000000 n=1,done!
> 1.000000 2.000000 3.000000 n=2,done!
> 1.000000 2.000000 3.000000 n=3,done!
> //////////////////////////////////////////
> Also if I use fscanf to read from the file:
> fscanf(pf,"%lf %lf %lf",a[n].x, a[n].y, a[n].z);
> I only get 0.0000000 for all the a[i] values.
>
> Thank you for helping!
>
> Lixia
> _______________________________________________
> AMBER mailing list
> AMBER.ambermd.org
> http://lists.ambermd.org/mailman/listinfo/amber
>



_______________________________________________
AMBER mailing list
AMBER.ambermd.org
http://lists.ambermd.org/mailman/listinfo/amber
Received on Wed Jan 27 2010 - 11:30:04 PST
Custom Search