Thursday, April 12, 2012

C - Trouble inputting doubles to array

I know C++ fairly well and was told to translate a program I had written in C++ to C for use in a different setting. I have never written code in plain C before and I am running into some trouble. The following function is supposed to read a .txt file and input the values within to a double array, however whenever I print the array, all values are: -92559631349317831000000000000000000000000000000000000000000000000000.00000. However if I manually change a value in the code before printing I get the correct value. Here is the input function:


    void FileRead(double *AttPos)
 {
 int j = 0;
 FILE *in_file;
 in_file = fopen("DragonData.txt", "r");
 
 if (in_file == NULL)
 {
     exit(1);
 }
 
 else
 {
     for(j; j<8; j++)
     {
         fscanf(in_file, "%lf", &AttPos[i]);
     }
 
     fclose(in_file);
 }
 }
 


And here is the input file:


12345
 0.13717085
 0.91813290
 0.73543816
 -0.67362091
 111
 -63
 11000
 



Answer:

I think it is due to a mistake in your code.

for(j; j<8; j++) { 
    fscanf(in_file, "%lf", &AttPos[i]); } 

That array index should be j not i, your values aren't actually being put into the array.

No comments:

Post a Comment