PDA

View Full Version : QFile and position



Rhian
27th September 2016, 23:11
I want to read a specific line from a textfile and save the position of the line.
In a next step I want to start the loop directly at the saved position (with seek).
With this I want to avoid to start the loop at the beginning of the textfile.
Is it possible to do this with file.pos() ?
It seems that pos always gives me the end of file position not the position of the last readLine.



file.readLine();
file.pos();


Is it also possible to have a pointer to the position of the last readLine?

Thanks for help.

d_stranz
28th September 2016, 15:56
If you call QIODevice::pos() after you call QIODevice::readLine(), it will report the position after the line you have read. If your file has only one line, then this will point to the end of the file.

If you want to be able to go back to the specific line, then you need to call pos() before you read the line, not after. If the line is not the one you want, then you loop, calling pos() and then readLine() until you get to the line you want. Save that position, and the next time you want to go to that line, call seek() using that position.