简体   繁体   中英

Reading %le from file

I'm trying to read 3 doubles from an input file with fscanf, but I'm getting a segfault when trying to do so. My code is as follows:

    FILE * infile = fopen(argv[1], "r");
    double r_d, r, c;
    fscanf(infile, "%le %le %le", r_d, r, c);
    fclose(infile);

The input file looks like this:

1.000000e+00 1.000000e+00 1.000000e+00

Any idea where I'm going wrong? I know for certain the data type is meant to be read as %le.

The scanf functions require a pointer to the objects you want populated with the values, so:

fscanf(infile, "%le %le %le", &r_d, &r, &c);
//                            ^     ^   ^

would be a much better start. Without the address-of operators, it will most likely use the current value of the variables (which, if they're static storage duration as they appear to be, will be arbitrary) as the pointer to write the values to. This is unlikely to end well :-)

I would imagine a decent compiler should have at least warned you that the types of the arguments were not as expected. If it did, you should get nto the habit of not ignoring such warnings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM