ifstream not reading EOF character

前端 未结 4 1793
不思量自难忘°
不思量自难忘° 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:17

    Do not do it like that.

    EOF is not the only thing you'll encounter while reading. There's a bunch of errors you might get, and so the best is to simply test the stream itself:

    while(currentfile)
    {
        // read somehow
    }
    

    If you're reading lines, then, the simplest way is:

    std::string line;
    while(std::getline(currentfile, line))
    {
        // use line
    }
    

提交回复
热议问题