QTextStream

编程入门 行业动态 更新时间:2024-10-24 02:38:22
QTextStream - position方法返回的确切内容(QTextStream - What exactly does the position method return)

我有一个关于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... */ } ... }

更多推荐

本文发布于:2023-08-01 16:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1361550.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:QTextStream

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!