带浮点数的 fscanf 输入
我正在从包含以下内容的文本文件中读取:
Mary 55334422 24.90 56.6 45.68
并正在读入:
....char name[20]; int num; double worked; double rate; double total;....
fscanf(fp, "%s %d %f %f %f\n", name, &num, &worked, &rate, &total);
我得到了名称和整数,但是浮点数的结果类似于 -9522999990000000000000000000.00
我在这里做错了什么吗?
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 wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用双精度格式:
%lf
,而不是浮点数%f
...或者更改为浮点数而不是双精度数。You need to use the format for a double:
%lf
, rather than that for a float%f
... or change to floats instead of doubles.尝试用
lf
而不是f
解析为双变量:Try
lf
instead off
to parse into double variables:将双精度数更改为浮点数,或将格式更改为 %lf
Change your doubles to floats, or change your format to %lf