【发布时间】:2012-01-24 12:03:21
【问题描述】:
我有一个格式如下的文件:
-0.0064785667 0.73900002 0.028505694 4.7858757e-39 315 218
-0.0051828534 0.73900002 0.028505694 4.6936954e-39 316 218
-0.0038818798 0.73799998 0.028467119 5.1546736e-39 317 218
-0.0025879198 0.73799998 0.028467119 5.6160217e-39 318 218
-0.0012939599 0.73799998 0.028467119 6.4461411e-39 319 218
我用这段代码读过它:
using namespace std;
ifstream inputFile;
//Opens data file for reading.
inputFile.open(filePath.c_str());
//Creates vector, initially with 0 points.
vector<Point> data(0);
float temp_x,temp_y,temp_z,rgb=0,discard_1=0,discard_2=0;
//Read contents of file till EOF.
while (inputFile.good()){
inputFile >> temp_x >> temp_y >> temp_z >> rgb >> discard_1 >> discard_2;
data.push_back(Point(temp_x,temp_y,temp_z,rgb));
}
if (!inputFile.eof())
if (inputFile.fail()) cout << "Type mismatch during parsing." << endl;
else cout << "Unknow problem during parsing." << endl;
//Close data file.
inputFile.close();
读取科学记数法浮点数会导致类型不匹配。
如何读取所有数字,包括科学记数法浮点数?
【问题讨论】:
-
您是否尝试将变量声明为双精度?
-
我cannot reproduce这个错误。您能否提供一个显示错误的完整程序(最好通过修改我发布的示例)?
-
尝试将 rgb 声明为 double,并且正在工作。但我希望能够直接读取浮点数......
-
如果你尝试读取一个大于最大浮点数(大约 3.40E38)的浮点数,你会得到一个失败状态。您在此处显示的值并非如此(它们应在浮点数中读取为 0),但可以解释为什么它适用于双精度而不适用于浮点数。
标签: c++ floating-point iostream scientific-notation