使用fscanf读取double [英] Read double using fscanf

查看:185
本文介绍了使用fscanf读取double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文本文件中读取double。
例如。
31 39.9316476397222 116.113516352222

I want to read double from text file e.g. 31 39.9316476397222 116.113516352222

我试过两个,不工作。我只能读取前几个十进制数字。 39.93164但不是39.9316476397222
任何人知道为什么?谢谢!

I tried both, not work. I can only read the first few decimal digital e.g. 39.93164 but not 39.9316476397222 anyone knows why? Thanks!

int NodeID;
double _lat,_long;
fscanf (pFile, "%d %lf %lf", &NodeID,&_lat,&_long);
printf ("I have read: %d %f %f\n", NodeID,_lat,_long);

fscanf (pFile, "%d %lf %lf", &NodeID,&_lat,&_long);
printf ("I have read: %d %lf %lf\n", NodeID,_lat,_long);


推荐答案

你的问题在于你的打印输出,这使你认为你没有整数。请记住, printf 通常只输出小数点后的几位数字。

I think the numbers are read correctly. Your problem is in your printing out, which makes you think you don't have the whole number. Keep in mind that printf typically only outputs a few digits after the decimal point.

我建议你这样做:


  1. 在您的 printf 呼叫中,请使用如下格式说明符:%。20f 。它会告诉 printf 输出小数点后的20位数字。

  2. 请记住,无论使用什么浮点数, float double long double 和分辨率。熟悉浮点数以及它们的工作方式和表示方式。

  1. In your printf calls, use a format specifier like this: "%.20f". It tells printf to output 20 digits after the decimal point.
  2. Keep in mind that whatever floating-point you use, float or double or long double, it's going to have a limited precision and resolution. Familiarize yourself with floating-point numbers and how they work and are represented.

这篇关于使用fscanf读取double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆