C reading values into double array, read values are different from values on local file

Advertisements

I’ve attempted to read a column of values in txt file into a double array in C (I haven’t written C/C++ for years, take me as a newbie). The values in file and the values in the double array were different. I don’t understand why and how to correct it.

I’ve locked C standard to C99 in CMAKE as well because other parts of my C codebase come from very old time (2006). But they are only after these values are correctly read.

Here’s the code snippet to do so:

    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(){
        double *input = malloc(sizeof(double) * 30);
        FILE *fReader= fopen("../values.txt", "r");
        if (fReader == NULL){
            perror("following error occurred:");
            return EXIT_FAILURE;
        }
        for(int i=0; i<30; i++){
        // If I use input[i] instead, it's causing a segfault.
            if(fscanf(fReader, "%f", (input+i)) != 1){
                perror("following error occurred during value reading: ");
                fclose(fReader);
                return EXIT_FAILURE;
            }
            printf( "%current value is %e \n", *(input+i));
        }
    
        if (ferror(fReader)){
            perror("following error occurred after reading: ");
            return EXIT_FAILURE;
        }
        if (fclose(fReader)){
            perror("following error occurred closing file: ");
            return EXIT_FAILURE;
        }
    
        return 0;
    }

This code ran ok, what’s interesting was the values read in were different from the values on file.

Here’s the output from gdb by doing p *input@30@1:

$1 = {
  {
    5.2090851696161347e-315,
    1.5708573496820653e-314,
    5.1267133989091838e-315,
    0,
    1.5717337030679671e-314,
    5.1214795787184985e-315,
    1.5632763856615201e-314,
    1.5698868046570466e-314,
    5.1603745953073066e-315,
    1.5776507290912971e-314,
    5.1890476209538565e-315,
    1.5757154067636091e-314,
    5.1701259393153366e-315,
    1.5646987621187767e-314,
    1.5706057487281833e-314,
    1.5745646547527293e-314,
    5.074800315787295e-315,
    1.5708606090331309e-314,
    1.5584251847289437e-314,
    5.0712624352138373e-315,
    1.5742053425473973e-314,
    5.121533268832232e-315,
    1.5713904213165801e-314,
    1.5767611663662882e-314,
    5.1426528410215284e-315,
    1.5729652822422439e-314,
    5.1593853918931727e-315,
    1.5747322610883588e-314,
    4.9742190195450526e-315,
    5.0023562211173747e-315
  }
}

And here’s the file content (called values.txt):

0.421499729344799
-0.0637534886782198
0.106157122495887
0
-0.0769690171367188
0.0982644454758486
-0.0186078658644284
-0.0558087634241033
0.188837376753259
-0.207397239912854
0.300632085420638
-0.149027358129317
0.218247717844644
-0.0239702769714967
-0.0612296530370315
-0.119660179409031
0.0451857273951361
-0.0638026393400715
-0.00797183286147529
0.0425181372831318
-0.114241700013729
0.0983454129367622
-0.0717922840839158
-0.18056777523574
0.135388066712405
-0.0955414085415107
0.185853919257983
-0.122187706120336
0.00796167779508749
0.0132655904045542

>Solution :

"%f" in scanf() is for reading float. Use "%lf" (add l) instead for reading double.

You can use &input[i] (not input[i] without &) instead of (input+i) to read values.

Also note that the line

printf( "%current value is %e \n", *(input+i));

invokes undefined behavior by

  • passing data having wrong type to printf(): %c requests int, but *(input+i) has type double.
  • not providing data corresponding to %e.

Leave a ReplyCancel reply