[GNHLUG] Silly C/C++ question...

Kevin D. Clark kevin_d_clark at comcast.net
Mon Dec 1 16:14:34 EST 2008


bruce.labitt writes:

> double fstart1, fend1, , M1, dt1 ;  int N;
> FILE *fp;
> fp = fopen("/home/workspace/input3.txt", "r"); // open input3 file for
> reading
> printf("input3.txt opened for reading.\n ");
> 
> fscanf(fp, "%i", &N);                                    // Find file
> length ==> read first line
> printf("file length is %i\n", N);                        // all is good
> 
> fscanf(fp, "%d%d%d%d", &fstart1, &fend1, &M1, &dt1);     // get fstart,
> fend, number of doppler slices & dt
> printf(" fstart = %d  fend = %d number of doppler slices = %d dt = %d \n",
> fstart1, fend1, M1, dt1);


fstart1 is a double.

In the context of fscanf(), "%d" specifies "read in the bytestream,
interpret it as an integer, and then stuff this integer value into
this integer pointer that the programmer has conveniently supplied".
But again, fstart1 is not an integer.  Try "%lf"...

I strongly suggest compiling all of your code with "g++ -Wformat" or
(even better) "-Werror -Wall -Wcast-qual".

Kind regards,

--kevin
-- 
GnuPG ID: B280F24E                Meet me by the knuckles
alumni.unh.edu!kdc                of the skinny-bone tree.
http://kdc-blog.blogspot.com/     -- Tom Waits


More information about the gnhlug-discuss mailing list