我有一个关于QTextStream用pos()方法计算什么的问题。 我假设它是字节数,但似乎可能不是这种情况。
我问的原因是,我正在处理文件中的行,一旦读取的行数达到某个任意数字或者stream.atEnd()为真,我就会跳出循环并将stream.pos()保存到qint64*变量。 一旦处理完成,我回到文件并seek(*filePosition)回到我的最后位置并获取更多数据,直到stream.atEnd()为真。 这可以跟踪我的位置,但调用stream.pos()非常慢,如Qt文档中所述。
我正在尝试的是在以有效的方式读取每一行之后更新文件位置。 但是,它不起作用,当程序返回再次读取文件时,位置不正确,因为它读取的第一行在先前迭代的先前读取的行的中间开始。
这是迄今为止的情况:
QTextStream stream(this); stream.seek(*filePosition); int ROW_COUNT = 1; while (!stream.atEnd()) { QString row = stream.readLine(); QStringList rowData = row.split(QRegExp(delimiter)); *filePosition += row.toUtf8().size(); /* processing rowData... */ if (ROW_COUNT == ROW_UPLOAD_LIMIT) { break; } ROW_COUNT++; } /* close files, flush stream, more processing, etc... */I have a question about what a QTextStream is calculating with the pos() method. I assumed it was the number of bytes, but it seems that this might not be the case.
The reason I ask, is that I am processing rows in a file, and once the number of rows read reached some arbitrary number or stream.atEnd() is true, I break out of the loop and save stream.pos() to a qint64* variable. Once the processing is complete, I go back to the file and seek(*filePosition) to get back to my last position and grab more data until stream.atEnd() is true. This works in the sense that can keep track of where I am, but it is very slow calling stream.pos() as is noted in the Qt docs.
What I am attempting is to update the file position after each line is read in an efficient manner. However, it is not working and when the program goes back to read the file again, the position is not correct as the first line it reads starts in the middle of line previously read on the last iteration.
Here is what is have so far:
QTextStream stream(this); stream.seek(*filePosition); int ROW_COUNT = 1; while (!stream.atEnd()) { QString row = stream.readLine(); QStringList rowData = row.split(QRegExp(delimiter)); *filePosition += row.toUtf8().size(); /* processing rowData... */ if (ROW_COUNT == ROW_UPLOAD_LIMIT) { break; } ROW_COUNT++; } /* close files, flush stream, more processing, etc... */最满意答案
QTextStream::pos以字节为单位返回位置。 我看到以下问题:
您没有考虑行结束字符(或2个字符) 在UTF-8中,单个字符可能需要超过1个字节另外,为什么在读取每一行后保存缓冲区位置? 这可能会更快:
if (ROW_COUNT == ROW_UPLOAD_LIMIT) { *filePosition = stream.pos(); break; }The solution was to create the QTextStream outside of the function and pass it in as a parameter. Doing this allows me to not have to worry about tracking the position on each iteration because I keep the stream in scope until I have completely finished processing the file.
class FeedProcessor { ... void processFeedFile() { IntegrationFile file("big_file.txt"); file.open(QIODevice::ReadOnly | QIODevice::Text); QTextStream stream(&file); while(!stream.atEnd()) { file.extractFileContents(&stream); /* do more processing with file */ } } ... } class IntegrationFile : public QFile { ... void extractFileContents(QTextStream* stream) { int ROW_COUNT = 1; while (!stream.atEnd()) { QString row = stream.readLine(); QStringList rowData = row.split(QRegExp(delimiter)); /* processing rowData... */ if (ROW_COUNT == ROW_UPLOAD_LIMIT) { break; } ROW_COUNT++; } /* close files, flush stream, more processing, etc... */ } ... }更多推荐
发布评论