C++ 跳过 std::istream 中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2581794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:59:20  来源:igfitidea点击:

Skip lines in std::istream

c++istream

提问by ufk

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

我正在使用 std::getline() 从 std::istream 派生类读取行,如何向前移动几行?

Do I have to just read and discard them?

我是否必须阅读并丢弃它们?

回答by Arthur P. Golubev

No, you don't have to use getline

不,您不必使用 getline

The more efficient way is ignoring strings with std::istream::ignore

更有效的方法是使用std::istream::ignore忽略字符串

for (int currLineNumber = 0; currLineNumber < startLineNumber; ++currLineNumber){
    if (addressesFile.ignore(numeric_limits<streamsize>::max(), addressesFile.widen('\n'))){ 
        //just skipping the line
    } else 
        return HandleReadingLineError(addressesFile, currLineNumber);
}

HandleReadingLineError is not standart but hand-made, of course. The first parameter is maximum number of characters to extract. If this is exactly numeric_limits::max(), there is no limit: Link at cplusplus.com: std::istream::ignore

HandleReadingLineError当然不是标准的而是手工制作的。第一个参数是要提取的最大字符数。如果这正是 numeric_limits::max(),则没有限制: cplusplus.com 上的链接:std::istream::ignore

If you are going to skip a lot of lines you definitely should use it instead of getline: when i needed to skip 100000 lines in my file it took about a second in opposite to 22 seconds with getline.

如果你要跳过很多行,你肯定应该使用它而不是 getline:当我需要在我的文件中跳过 100000 行时,它花了大约一秒钟,而 getline 只需要 22 秒。

回答by Billy ONeal

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

编辑:您也可以使用 std::istream::ignore,请参阅https://stackoverflow.com/a/25012566/492336



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

我是否必须使用 getline 我想跳过的行数?

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.

不,但对于那些阅读您的代码的人来说,这可能是最清晰的解决方案。如果您跳过的行数很大,您可以通过读取大块并计算每个块中的换行符、停止文件并将其重新定位到最后一个换行符的位置来提高性能。但是除非您遇到性能问题,否则我只会将 getline 放入循环中以获取您要跳过的行数。

回答by Brian R. Bondy

Yes use std::getlineunless you know the location of the newlines.

是的,std::getline除非您知道换行符的位置。

If for some strange reason you happen to know the location of where the newlines appear then you can use ifstream::seekgfirst.

如果由于某种奇怪的原因您碰巧知道换行符出现的位置,那么您可以ifstream::seekg先使用。

You can read in other ways such as ifstream::readbut std::getlineis probably the easiest and most clear solution.

您可以通过其他方式阅读,例如ifstream::readstd::getline可能是最简单和最清晰的解决方案。

回答by GManNickG

For what it's worth:

物有所值:

void skip_lines(std::istream& pStream, size_t pLines)
{
    std::string s;
    for (; pLines; --pLines)
        std::getline(pStream, s);
}