Hi everyone,

I'm having issues while attempting to read a text file of the following structure

0 0 -7.278289
0 1 1.922087

As you see the first and second values in a line are integers, I'm not interested in those, the third value is a float which I want.

Basically I must read this file in two FOR loops, the master loop turns 10 times and for each of those iterations an "internal" loop runs another 10 times - here I extract the values and store them in an array

FILE *fp;
int i,j,k, i_in,j_in;
double p_in;

fp = fopen(fname, "r");
nn = 10;
for (i=0;i<nn;++i){
	for (j=0;j<nn;++j){
		fscanf(fp,"%d %d %lg", &i_in, &j_in, &p_in);
		pop[0][0][k] = p_in;
		printf("i_in: %d \n j_in: %d \n Value: %lg \n", &i_in,&j_in,&p_in);
		printf("i: %d\n j: %d \n k: %d \n",i,j,k);

This should load my values

but instead I get
i_in: -1073744740 which should be between 0 and 9
j_in: -1073744744 which should be between 0 and 9
VALUE: -1.997212 which should be the actual value, which it's not

The loops are working, the iterations are correct, it's just the extraction and storing of values that fails miserably and I fail to see the problem, the data types seem OK, I just don't get it.
Can someone please tell me what I'm doing wrong?


8 Years
Discussion Span
Last Post by Narue

I would do it step by step

1) read in a file
2) read in a line
3) split the line into char arrays delimited by the space
4) Grab the last value and check if it is a double then convert to double/float

Job done and it is more reliable than reading them in as doubles (error checking)


Add error checking then try again (probably fopen failed):

fp = fopen(...);
if (!fp) {
   /* Can't open ...*/
   if (fscanf(fp,"%d %d %lf",...) != 3) {
      /* Bad line... */

Are you sure you wanted pop[0][0][k] ? May be [i][j][k] ?..


Thanks for your help guys,

Double, triple and quadruple checked it,
- fopen works, file loads, I can print out the content to stdout,
- checked for num_of_elements in line - is correct
- The array is set up correctly, I've got plenty of other data that has to be loaded into this baby, but I've commented it all out and just stuck to this one

- the values are still crap... ;)

There has to be a mistake in format conversion, but all I have in there are integers and a double... I don't get it.
I'll rewrite it as iamthwee suggested, I'd rather not, but I'm curious as hell if the same problem will pop up.


>printf("i_in: %d \n j_in: %d \n Value: %lg \n", &i_in,&j_in,&p_in);
You're printing the value of the address of each of those variables. Remove the address-of operator.


Hi Narue, thanks for taking the time to answer me :)

Unfortunately if I remove the ampersand the program produces a bus error :(
I thought that's because it attempts to access a memory location outside its address space.


Remove it from printf, but not scanf. Like this:

fscanf(fp,"%d %d %lg", &i_in, &j_in, &p_in);
/* ... */
printf("i_in: %d \n j_in: %d \n Value: %lg \n", i_in,j_in,p_in);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.