开发者

fscanf input with floats

开发者 https://www.devze.com 2023-02-14 10:06 出处:网络
I\'m reading from a text file which contains: Mary 55334422 24.90 56.6 45.68 and am reading it in: ....char name[20]; int num; double worked; double rate; double total;....

I'm reading from a text file which contains:

Mary 55334422 24.90 56.6 45.68

and am reading it in:

....char name[20]; int num; double worked; double rate; double total;....

fscanf(fp, "%s %d %f %f %f\n", name, &num, &worked, &rate, &total);

I'm getting the name and the integer fine, but the floating point numbers come out something like -9522999990000000000000000000.00

Am I doing something wr开发者_JS百科ong here?


You need to use the format for a double: %lf, rather than that for a float %f... or change to floats instead of doubles.


Try lf instead of f to parse into double variables:

fscanf(fp, "%s %d %lf %lf %lf\n", name, &num, &worked, &rate, &total);


Change your doubles to floats, or change your format to %lf

0

精彩评论

暂无评论...
验证码 换一张
取 消