【发布时间】:2019-05-19 12:27:13
【问题描述】:
基本上,如果我调用QFile::readLine,QFile 的整行将被复制并粘贴到char* 或QByteArray 中。如果我想跳过 999 行直接进入感兴趣的行(第 1000 行),那么我将无缘无故地复制和粘贴前 999 行,而我只想跳过它们。
我知道istream::ignore 允许用户跳过任意数量的字符,直到找到分隔符,所以
std::ifstream file("file.txt");
for (auto i = 0u; i < 999u; ++i)
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::string str;
getline(file, str); // The 1,000th line is copied & pasted into str
会让您直接进入第 1000 行,而不会浪费任何时间进行复制和粘贴。我怎样才能用QFile 做同样的事情?
【问题讨论】:
-
你可以循环调用
getChar,直到你看到999个换行符。