C++ 如何从文件C+中读取带科学符号的浮点+;?

C++ 如何从文件C+中读取带科学符号的浮点+;?,c++,floating-point,iostream,scientific-notation,C++,Floating Point,Iostream,Scientific Notation,我有一个以下格式的文件: -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.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();
使用名称空间std;
ifstream输入文件;
//打开数据文件进行读取。
open(filePath.c_str());
//创建向量,最初为0点。
矢量数据(0);
浮动温度x,温度y,温度z,rgb=0,丢弃温度1=0,丢弃温度2=0;
//读取文件内容直到EOF。
while(inputFile.good()){
输入文件>>温度x>>温度y>>温度z>>rgb>>丢弃\U 1>>丢弃\U 2;
数据。向后推(点(温度x、温度y、温度z、rgb));
}
如果(!inputFile.eof())

如果(inputFile.fail())不能问题可能是您的while循环。切勿在(inputFile.good())
时使用
。使用以下命令:

while (inputFile >> temp_x >> temp_y >> temp_z 
                 >> rgb >> discard_1 >> discard_2) {}
另一个问题解释了原因。您可能还对它感兴趣,它建议了一个更优雅的解决方案。

用一个没有任何错误处理的示例展开:

#包括
#包括
#包括
#包括
结构点
{
双x,y,z,rgb;
};
结构点线:公共点
{
int丢弃[2];
};
std::istream&operator>>(std::istream&is,点线和输入)
{
is>>in.x>>in.y>>in.z>>in.rgb>>in.discard[0]>>in.discard[1];
//TODO:添加错误句柄
回报是;
}

std::ostream&operator您是否尝试将变量声明为double?我发现了这个错误。你能提供一个完整的程序来显示错误吗(最好是修改我发布的示例)?尝试将rgb声明为double,并且正在工作。但我希望能够直接读取浮点值……如果尝试读取大于最大浮点值(约3.40E38)的浮点值,就会出现失败状态。这里显示的值不是这种情况(它们在浮点中应被读取为0),但可以解释为什么它与double而不是float一起工作。发布完整的代码后,我将尝试您的修复,看看它是否有效。@Luis:看到您的代码,我想您可能希望以不同的方式处理此问题,正如我回答中的第二个链接所述。@Luis:到底发生了什么?你能提供一个完整的例子吗?您是否知道,您的程序将在解析期间打印“类型不匹配”。
即使一切正常?抱歉,更正了else语句,如果它达到EOF,它将不会打印任何错误消息。只有当它没有到达EOF时,才会打印错误消息。在你的第二个链接中,你将如何丢弃类中不需要的内容,如discard_1和2?这是我第一次将切片视为有意且有用的效果。虽然我想知道,这是允许的吗?它会产生内存泄漏吗?我不确定。我想这是我第一次以这种方式使用它:-)它确实有效,
点线
在被切成
向量
后会被破坏,所以我认为没有内存泄漏。也许我应该把这当作一个问题来问……不过,仔细想想,我不认为有记忆问题。它将简单地调用
的复制构造函数,并引用
点_行
,这很好。@BjörnPollex:是的,这是我的想法。
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>

struct point
{
    double x, y, z, rgb;
};

struct point_line: public point
{
    int discard[2];
};

std::istream& operator >>(std::istream& is, point_line& in)
{
    is >> in.x >> in.y >> in.z >> in.rgb >> in.discard[0] >> in.discard[1];
    // TODO: Add your error-handlin

    return is;
}

std::ostream& operator <<(std::ostream& os, const point& out)
{
    os << " X: " << out.x
       << " Y: " << out.y
       << " Z: " << out.z
       << " RGB: " << out.rgb
       ;

    return os;
}

int main()
{
    std::ifstream points_file("points.txt");
    std::vector<point> points;
    std::copy(std::istream_iterator<point_line>(points_file),
              std::istream_iterator<point_line>(),
              std::back_inserter(points));

    std::copy(points.begin(),
              points.end(),
              std::ostream_iterator<point>(std::cout, "\n"));
}