本文介绍了如何从文件C ++用科学记数法读取浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这种格式的文件:
-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();
阅读类型不匹配的科学记数法浮动的结果。
Reading the scientific notation float results in a type mismatch.
我怎样才能阅读所有数字,包括科学记数法浮?
How can I read all the numbers including the scientific notation float?
推荐答案
这个问题可能是你在─循环。从不使用 while(inputFile.good())
。使用这样的:
The problem is probably your while-loop. Never use while(inputFile.good())
. Use this:
while (inputFile >> temp_x >> temp_y >> temp_z
>> rgb >> discard_1 >> discard_2) {}
的以一个不同的问题解释了原因。您可能也有兴趣,这表明一个更优雅的解决方案。
This answer to a different question explains why. You might also be interested in this answer, which suggests a more elegant solution.
这篇关于如何从文件C ++用科学记数法读取浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!