简体   繁体   中英

How to save QTextStream in Qt?

I have to read a text file in Qt line by line.

So I made a function that reads first X lines of the file. But when the function is called next time I want the reading to start from line X + 1 . I know that I can do this just by skipping the first X lines.

But I tried to save the QTextStream object which gives me the error:

in expansion of macro Q_DISABLE_COPY .

and if I save the pointer to QTextStream object, then my application hangs. Does this mean that skipping first X lines is the only way to do this?

You can save the stream's current position using pos() and then seek(position) to resume from the previous reached point.

I suspect your QTextStream is created on the stack each time you read, which is why if you try using a pointer to it the next time it crashes, because it is a dangling pointer pointing to a no longer valid object.

So you either need to make the text stream persistent, so either implement it is a member variable or allocate it on the heap with new which will work with a pointer, or simply create a new text stream and seek to the previous position.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM