在 C++ 中未读取科学的“d”符号

use*_*935 0 c++ fortran scientific-notation data-files

我需要读取一个数据文件,其中的数字以如下格式写入:

1.0d-05
Run Code Online (Sandbox Code Playgroud)

C++ 似乎无法识别这种类型的科学记数法!关于如何读取/转换这些类型的数字的任何想法?

我需要数字(即double/ float)而不是字符串。也许已经有一个类/头来管理这种格式,但我找不到它。

man*_*lio 5

Fortran 程序生成的文件使用字母 D 而不是 E报告双精度数字(以科学计数法)

所以你的选择是:

  1. 预处理 Fortran 数据文件(简单的搜索和替换就足够了)。
  2. 使用类似的东西:

    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    
    int main()
    {
      std::istringstream input("+1.234000D-5 -2.345600D+0 +3.456700D-2");
    
      std::vector<double> result;
    
      std::string s;
      while (input >> s)
      {
        auto e(s.find_first_of("Dd"));
        if (e != std::string::npos)
          s[e] = 'E';
    
        result.push_back(std::stod(s));
      }
    
      for (auto d : result)
        std::cout << std::fixed << d << std::endl;
    
      return 0;
    }
    
    Run Code Online (Sandbox Code Playgroud)

还: