在不移动文件位置的情况下从文件中读取下一个字符串的快速方法

Quick way to read the next string from a file without moving the position of the file

我想知道在不增加光标在文件中的位置的情况下读取文件中的下一个字符串(检查它是否与字符串匹配)的最佳方法是什么。所以基本上我想在不读取下一个字符串的情况下读取下一个字符串。

只要使用相同的 ifstream 对象,不改变光标就无法读取下一个字符串,但可以保存和恢复位置:

auto p = stream.tellg();
// do what ever read you like
stream.seekg(p, std::ios_base::beg);

如果读取失败,您可能必须在调用 seekg() 之前清除错误标志。