【问题标题】:Using ifstream to read floats使用 ifstream 读取浮点数
【发布时间】:2014-04-01 18:43:04
【问题描述】:

我正在尝试使用 ifstream 从 .out 文件中读取一系列浮点数,但如果我之后输出它们,它们是不正确的。

这是我的输入代码:

float x, y, z;

ifstream table;
table.open("Resources/bones.out");
if (table.fail())
{
    cout << "Can't open table" << endl;
    return ;
}

table >> x;
table >> y;
table >> z;

cout << x << " " << y << " " << z << endl;

table.close();

我的输入文件:

0.488454 0.510216 0.466979
0.487242 0.421347 0.472977
0.486773 0.371251 0.473103
...

现在进行测试,我只是将第一行读入 x yz,我的输出是

1 0 2

关于为什么我没有得到正确输出的任何想法?

【问题讨论】:

  • 它工作正常,我刚刚测试过。打印“0.488454 0.510216 0.466979”。因此,您未显示的代码可能有问题。发布一个完整的、可编译的示例。还请告诉我们您的编译器以及您是如何调用它来编译代码的。
  • 我不能 100% 确定 fail() 会告诉你是否无法打开文件。流上的错误标志有点繁琐。我认为在使用[io]fstream 之前检查它的状态的“最佳”方法就是if (!myStream) // something went wrong
  • if (table &gt;&gt; x &gt;&gt; y &gt;&gt; z) {...} else { std::cout &lt;&lt; "failed to read x,y,z"; }
  • @WhozCraig 或者更好的是,先用 std::getline 读一整行,然后在行上使用 std::istringstream 来获取各个标记,这样就可以产生更精确的错误消息:)
  • 是因为我错误地读取了 .out 文件,还是我假设它与 .txt 文件的读取方式相同?

标签: c++ io ifstream


【解决方案1】:
#include <fstream>
#include <strtk.hpp>   // http://www.partow.net/programming/strtk

std::string filename("Resources/bones.out");

// assuming the file is text
std::fstream fs;
fs.open(filename.c_str(), std::ios::in);

if(fs.fail())  return false;   

const char *whitespace    = " \t\r\n\f";

std::string line;
std::vector<float> floats;
std::vector<std::string> strings;
float x = 0.0, y = 0.0, z = 0.0;
std::string xs, ys, zs;

// process each line in turn
while( std::getline(fs, line ) )
{
    // Removing beginning and ending whitespace
    // can prevent parsing problems from different line endings.
    // formerly accomplished with boost::algorithm::trim(line)

    strtk::remove_leading_trailing(whitespace, line);


    // strtk::parse combines multiple delimiters in these cases

    if( strtk::parse(line, whitespace, floats ) ) 
    {
         std::cout << "succeed" << std::endl;
         // floats contains all the values on the in as floats
    }

    if( strtk::parse(line, whitespace, strings) ) 
    {
         std::cout << "succeed" << std::endl;
         // strings contains all the values on the in line as strings
    }

    if( strtk::parse(line, whitespace, x, y, z) ) 
    {
         std::cout << "succeed" << std::endl;
         // x,y,z contain the float values.  parse fails if more than 3 floats are on the line
    }

    if( strtk::parse(line, whitespace, xs, ys, zs) ) 
    {
         std::cout << "succeed" << std::endl;
         // xs,ys,zs contain the strings.  parse fails if more than 3 strings are on the line
    }
}

这就是我将如何解决它。您可以选择自己的方式来解析数据。

【讨论】:

    【解决方案2】:

    我之前已经处理过这个问题,我想做的事情就像下面的代码,您可以在其中逐行读取文本文件并使用 getline 和字符串将 twext 放入变量中。您不必使用数组,因为它仅限于元素,而是使用向量,这样您就可以动态添加。

        string xs;
        string ys;
        string zs;
        ifstream infile;
        someArray[50];
        infile.open("some file.txt");
    
        if (!infile)
        {
            cout << "no good file failed! \n" << endl;
        }
    
        while (infile.good())
        {
            for (int i = 0; i < 49; ++i)
            {
                getline(infile, xs);
                //Saves the line in xs.
                    infile >> p[i].xs;
    
                getline(infile, ys, ',');
                infile >> p[i].ys;
                getline(infile, zs, ',');
                infile >> p[i].zs;
    
            }
            //infile >> p.fromFloor; */
    
    
    
        }
    
        infile.close(); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-09
      • 2013-10-15
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 2017-08-16
      相关资源
      最近更新 更多