0
我试图逐字读取char buffer
中的double
值。在该文件中我有一个像0.032 0.1234 8e-6 4e-3
等一些值这里是我的代码,这是使用atof()
功能转换成Word(存储在阵列“s
”)为双精度值num
:atof()无法在8e-6中读取'e'
char s[50];
double num;
while(getline(str, line))
{
int i=0, j=0;
while(line[i] != '\0')
{
if(line[i] != ' ')
{
s[j] = line[i];
//cout << s[j]; <-- output of s[j] shows correct values (reading 'e' correctly)
j++;
}
else
{
num = atof(s);
cout << num << " ";
j=0;
}
i++;
}
cout << endl;
}
每当数字,其中包括“E '在他们(如8e-6)来,atof()函数只为他们返回0。以上示例值的输出结果为0.032 0.1234 0 0
。
我试图检查演示数组,atof()工作正常。
char t[10];
t[0] = '8';
t[1] = 'e';
t[2] = '-';
t[3] = '5';
t[4] = '\0';
double td = atof(t);
cout << "td = " << td << endl;
这里输出td = 8e-05
我既包括<stdio.h>
和<stdlib.h>
。任何想法为什么它不工作?
你期待什么答案? –
我期待'0.032 0.1234 8e-06 4e-03'。 – Jaipreet
请学习如何调试。这很简单,是你现在需要的最重要的技能。 – Amit