Results 1 to 2 of 2

Thread: QFile and position

  1. #1
    Join Date
    Apr 2014
    Posts
    3

    Default QFile and position

    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.

    Qt Code:
    1. file.readLine();
    2. file.pos();
    To copy to clipboard, switch view to plain text mode 

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

    Thanks for help.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFile and position

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 2
    Last Post: 6th May 2013, 08:06
  2. view position of mouse position in GraphicsScene
    By Raghaw in forum Qt Programming
    Replies: 2
    Last Post: 23rd August 2012, 04:46
  3. qfile read from last position
    By NathiyaAmose in forum Qt Programming
    Replies: 4
    Last Post: 23rd July 2012, 10:21
  4. Replies: 4
    Last Post: 9th May 2011, 09:52
  5. QFile &QFile::operator= is private
    By Fallen_ in forum Newbie
    Replies: 1
    Last Post: 15th March 2011, 15:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.