ifstream, end of line and move to next line?

﹥>﹥吖頭↗ 提交于 2019-12-03 07:42:06

Use ignore() to ignore everything until the next line:

 in.ignore(std::numeric_limits<std::streamsize>::max(), '\n')

If you must do it manually just check othe character to see if is '\n'

char next;
while(in.get(next))
{
    if (next == '\n')  // If the file has been opened in
    {    break;        // text mode then it will correctly decode the
    }                  // platform specific EOL marker into '\n'
}
// This is reached on a newline or EOF

This is probably failing because you are doing a seek before clearing the bad bits.

in.seekg(0, ios::beg);    // If bad bits. Is this not ignored ?
                          // So this is not moving the file position.
sz.clear();
getline(in, sz);
cout << sz <<endl; //no longer reads

You should clear the error state of the stream with in.clear(); after the loop, then the stream will work again as if no error happened.

You might also simplify your loop to:

while (in >> v) {
  cout << v << " ";
}
in.clear();

The stream extraction returns if the operation succeeded, so you can test this directly without explicitly checking in.good();.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!