Skip lines in std::istream

后端 未结 4 997
我在风中等你
我在风中等你 2021-01-12 03:05

I\'m using std::getline() to read lines from an std::istream-derived class, how can I move forward a few lines?

Do I have to just read and discard them?

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-12 03:31

    Edit: You can also use std::istream::ignore, see https://stackoverflow.com/a/25012566/492336


    Do I have to use getline the number of lines I want to skip?

    No, but it's probably going to be the clearest solution to those reading your code. If the number of lines you're skipping is large, you can improve performance by reading large blocks and counting newlines in each block, stopping and repositioning the file to the last newline's location. But unless you are having performance problems, I'd just put getline in a loop for the number of lines you want to skip.

提交回复
热议问题