我必须解析一个包含数千条记录的大文件。我想显示一个进度条,但是我不知道提前录制的数量。我是否必须将此文件翻转两次?
还是有一种更简单的方法来获取记录数,而无需在处理之前遍历整个文件?
我的代码段:
void readFromCSV(const QString &filename){
int line_count=0;
QString line;
QFile file(filename);
if(!file.open(QFile::ReadOnly | QFile::Text))
return;
QTextStream in(&file);
while(!in.atEnd()){ //First loop
line = in.readLine();
line_count++;
}
while (!in.atEnd()) { //Second loop
...
line = in.readLine();
process();
...
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
这个问题与这里的问题不同:计算文本文件中的行数
1)循环过程已经完成。在这种情况下,可以防止双重射击。
2)该代码是QT代码,而不是作为冗余添加的C ++功能