c++ - 使用 fscanf 读取双倍

标签 c++ c file scanf

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

我都试过了,都不行。我只能读取前几个十进制数字,例如39.93164 但不是 39.9316476397222 有谁知道为什么?谢谢!

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);

最佳答案

根据fscanf man page , 你应该使用 %lf 来加倍 :

f, e, g (Floating point number) : A series of decimal digits, optionally containing a decimal point, optionally preceeded by a sign (+ or -) and optionally followed by the e or E character and a decimal integer (or some of the other sequences supported by strtod). Implementations complying with C99 also support hexadecimal floating-point format when preceded by 0x or 0X.
enter image description here

关于c++ - 使用 fscanf 读取双倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17852269/

相关文章:

Bison 的冲突

c - 将控制权从线程返回到调度程序(上下文切换)

html - go - 打开文件没有那个文件或目录

android - Xamarin 表格 : Write a file in Android device's internal storage

c++ - 崩溃与 "glibc detected: vector double free or corruption (out)"

c++ - 如何使用 CMake 将 vtk 库链接到多个源文件?

C++ 从结构数组中得到错误的值

c - 尝试使用 matlab 的 libmat.dll,但编译器无法识别库中的函数

c++ - 从外部文件获取输入?

c++ - 读取 iostream 直到找到字符串定界符