Sumeet
Sumeet

Reputation: 8292

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?

Upvotes: 0

Views: 316

Answers (1)

dtech
dtech

Reputation: 49329

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.

Upvotes: 2

Related Questions