istringstream不在变量中存储任何内容

istringstream not storing anything in variables

本文关键字:存储 任何内 变量 istringstream      更新时间:2023-10-16

istringstream没有存储它读取的值,我遇到了一个问题。这是我所拥有的:

      if(inputFile.good()){                                         //Make sure file is open before trying to work with it
                                                                    //Begin Working with information
        cout << "tIn File:  " << input << endl;
        cout << "------------------------------------" << endl;
        int number_of_lines = 0;
        std::string line;
        while (std::getline(inputFile, line)){
            ++number_of_lines;
        }
        Time times[number_of_lines];
        double math[number_of_lines];
        std::string input;
        int hh, mm;
        for(int loop=0;loop<number_of_lines;loop++){
            std::getline(inputFile, input);
            std::istringstream(input) >> mm >> hh >> math[loop];
            cout << "hours = " << hh << endl;
            times[loop].setTimeHours(hh);
            times[loop].setTimeMinutes(mm);
            times[loop].show();
            cout << "*" << math[loop] << endl;
        }
        std::cout << "Number of lines in text file: " << number_of_lines << "n" << endl;
    }else{
        cout << "Could not open file!!!" << endl;
    }

我正在阅读的文件如下:

90 1 3.0
1 1 100.0
2 34 5.1

以及我运行时的输出:

  In File:  data04.txt
 ------------------------------------
 hours = 0
 Operation To Be Done = 0:2336552*1.15384e-317
 hours = 0
 Operation To Be Done = 0:2336552*1.58101e-322
 hours = 0
 Operation To Be Done = 0:2336552*1.15397e-317
 Number of lines in text file: 3

有人知道它为什么不存储值吗?

这个代码中有几个关键问题

  1. 它不会检查输入是否成功。始终需要确保在处理读取的数据之前验证输入操作是否有效。否则将导致随机数据被处理
  2. 您首先阅读流的末尾,然后希望流神奇地重新启动。那行不通。只读取一次流,然后继续追加到std::vector<Time>(或类似的容器)。除了只遍历一次文件外,在UNIX上,读取时文件大小也会发生变化
  3. C++没有可变大小的数组,尽管有些编译器可能提供类似于C的可变大小数组的扩展。在C++中,您应该使用std::vector<Time>

首先,您的程序是错误的。while循环结束后,文件中没有更多可读取的内容(除非您将seekg()返回到开头),因此for循环体中的std::getline()调用基本上不起任何作用。

第二个问题是关注点没有适当地分开。

以下是我将如何实现这个程序:

struct line_data
{
  Time   t;
  double x;
};
// This handles reading a single Time value.
std::istream & operator >> (std::istream & is, Time & t)
{
  int hh, mm;
  if (is >> hh >> mm)
  {
    // Not happy with the following two lines, too Java-like. :-(
    t.setTimeHours(hh);
    t.setTimeMinutes(mm);
  }
  return is;
}
// This handles reading a single line of data.
std::istream & operator >> (std::istream & is, line_data & ld)
{
  std::string s;
  if (std::getline(is, s))
  {
    std::istringstream iss(s);
    // Ensure errors are propagated from iss to is.
    if (!(iss >> ld.t >> ld.x))
      is.setstate(std::ios::failbit);
  }
  return is;
};
// This handles processing a single line of data.
struct line_manip // satisfies concept OutputIterator<line_data>
{
  std::back_insert_iterator<std::vector<Time>>   ti;
  std::back_insert_iterator<std::vector<double>> xi;
  line_manip(std::vector<Time> & ts, std::vector<double> & xs)
    : ti(std::back_inserter(ts))
    , xi(std::back_inserter(xs))
  {
  }
  line_manip & operator = (const line_data & ld)
  {
    ti = ld.t;
    xi = ld.x;
    return *this;
  }
  line_manip & operator *  ()    { return *this; }
  line_manip & operator ++ ()    { return *this; }
  line_manip & operator ++ (int) { return *this; }
};
int main()
{
  std::ifstream ifs("input.txt");
  std::vector<Time>   ts;
  std::vector<double> xs;
  std::copy(std::istream_iterator<line_data>(ifs),
            std::istream_iterator<line_data>(),
            line_manip(ts, xs));
  // ...
}