ifstream not reading EOF character

前端 未结 4 1794
不思量自难忘°
不思量自难忘° 2020-12-01 20:13

I am creating a program (In C++) that takes an ASCII file and reads a few values from each line until it reaches the end of the file. I am using ifstream to re

4条回答
  •  醉酒成梦
    2020-12-01 21:07

    The problem is here:

    if (!curfile.good())
        cout<<"Bad file read"<

    Try this:

    void readFile(std::istream& str)
    {   
        std::string     line;
        while(std::getline(str, line))
        {
            std::stringstream   lineStream(line);
            std::string         ignoreWord;
            int                 number[3];
    
            lineStream >> ignoreWord   // reads one space seporated word
                       >> number[0]    // reads a number
                       >> ignoreWord >> ignoreWord >> ignoreWords  // reads three words 
                       >> number[1]    // reads a number
                       >> number[2];   // reads a number
    
            current.push_back(number[0]);
            dx.push_back(number[1]);
            dy.push_back(number[2]);
        }   
    }   
    

提交回复
热议问题