从 CSV C++中提取双精度值时出现意外结果



我在使用 C++ Qt 读取.csv文件时遇到问题。以下是 CSV 格式:

0.2345;0.567;1.2456;...

这是读取CSV文件的代码:

void CSV::readCSV(std::istream &input, std::vector<double> &output) {
  std::string csvElement;
  while (std::getline(input, csvElement, ';')) {
    output.push_back(stod(csvElement));
  }
}

和输出:

0.0000 0.0000 1.0000

输出错误。你能帮我解决这个问题吗?

我怀疑您在代码中的某个地方将值转换为某种整数类型,然后将它们打印出来或转换回双精度。类似于:

std::cout << std::fixed << std::setprecision(4);
for (auto el : vec){
    int i = el;
    double d = i;
    std::cout << "Double: " << el << " Integer: " << i << " Double again: " << d << std::endl;
}

最新更新