C++ istringstream未在变量中存储任何内容

C++ istringstream未在变量中存储任何内容,c++,file,variables,C++,File,Variables,istringstream没有存储它读取的值,我遇到了一个问题。以下是我所拥有的: if(inputFile.good()){ //Make sure file is open before trying to work with it //Begin Working w

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;
    }
以及运行时的输出:

  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

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

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

  • 它不会检查输入是否成功。在处理读取的数据之前,您始终需要确认输入操作是否有效。否则将导致处理随机数据
  • 您首先读取到流的末尾,然后希望流神奇地重新启动。那不行。只读取一次流,并将其添加到
    std::vector
    (或类似容器)中。除了只遍历文件一次之外,在unix上,文件大小可以在读取时更改
  • C++没有可变大小的数组,尽管有些编译器可能提供类似于C的可变大小数组的扩展。在C++中,你将使用<代码> STD::vector < /C> > .< /LI>
    首先也是最重要的,你的程序是错误的。在
    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));
      // ...
    }
    
    struct line\u数据
    {
    时间t;
    双x;
    };
    //这将处理读取单个时间值的操作。
    std::istream&operator>>(std::istream&is,Time&t)
    {
    int hh,mm;
    如果(是>>hh>>mm)
    {
    //对以下两行不满意,太像Java了:-(
    t、 设置时间小时(hh);
    t、 设置时间分钟(毫米);
    }
    回报是;
    }
    //这将处理读取单行数据的操作。
    std::istream&operator>>(std::istream&is,行数据与ld)
    {
    std::字符串s;
    if(std::getline(is,s))
    {
    标准:istringstream iss;
    //确保错误从iss传播到is。
    如果(!(iss>>ld.t>>ld.x))
    is.setstate(std::ios::failbit);
    }
    回报是;
    };
    //这将处理一行数据的处理。
    结构行\u manip//满足概念输出
    {
    std::back\u insert\u迭代器ti;
    席德:迭插入迭代器XI;
    行操作(标准::向量和ts,标准::向量和xs)
    :ti(标准:背面插入器(ts))
    ,xi(标准:背面插入器(xs))
    {
    }
    行操作器和运算符=(常量行数据和ld)
    {
    ti=ld.t;
    席= LD。
    归还*这个;
    }
    行操作符*(){return*this;}
    行_manip&operator++(){return*this;}
    行_manip&operator++(int){return*this;}
    };
    int main()
    {
    std::ifstream ifs(“input.txt”);
    std::向量ts;
    std::向量xs;
    std::copy(std::istream_迭代器(ifs),
    std::istream_迭代器(),
    线(ts,xs);;
    // ...
    }
    
    您是否检查了读取操作是否成功?(当然,只是一个由代码回答的修辞问题)我只是做了一个
    cout Yes。这不算验证输入是否成功。类似于
    if(std::getline(inputFile,line))
    while(std::getline(inputFile,line))
    if(std::istringstream(intput)>>mm>>hh)
    将被视为验证。啊!我想我在前面进行行计数时移到了文件的末尾!哈哈,哇,我需要进行验证,但你帮我修复了它,我忘记了在执行下一个for循环之前移回文件的开头。现在我将添加验证。谢谢!
    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));
      // ...
    }