我有这种格式的文件:

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

读取科学计数法浮点数会导致类型不匹配。

如何读取所有数字,包括科学计数法浮点数?

最佳答案

问题可能出在您的while循环中。切勿使用while(inputFile.good())。用这个:

while (inputFile >> temp_x >> temp_y >> temp_z
                 >> rgb >> discard_1 >> discard_2) {}

This answer向另一个问题解释了原因。您可能还对this answer感兴趣,它建议了一个更优雅的解决方案。

关于c++ - 如何从文件C++中读取具有科学计数法的float?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8986488/

10-11 22:38
查看更多