Results 1 to 3 of 3

Thread: QTextStream::seek() not working

  1. #1
    Join Date
    Sep 2006
    Posts
    38
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QTextStream::seek() not working

    Hello,

    I'm writing a program in Qt 5.1.1 that reads in a large, specifically formatted text file such as:

    Qt Code:
    1. Line1
    2. Line2
    3. Heading 1
    4. sub head 1
    5. sub head 2
    6. sub head 3
    7. Heading 2
    8. sub head 1
    9. sub head 2
    10. Heading 3
    11. ...
    To copy to clipboard, switch view to plain text mode 

    I want my program to perform functions on the sub heading lines within each "heading" section. There is data before and after the "Heading" sections in the file that I just want to ignore.

    Here's what I have (simplified):
    Qt Code:
    1. QFile file(fileName);
    2. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
    3. QTextStream in(&file);
    4. while (! in.atEnd() && !abort) //abort is used in a progress dialog and gets set when the user clicks 'cancel'
    5. {
    6. qApp->processEvents(); //to catch the 'cancel'
    7. str = in.readLine();
    8. if (str.section(" ",0,0) == "Heading")
    9. {
    10. qDebug()<<str;
    11. str = in.readLine();
    12. qint64 pos= in.pos();
    13. while (str.section(" ",0,0) == "")
    14. {
    15. ...
    16. //process sub head line
    17. ...
    18. str = in.readLine();
    19. qDebug()<<str;
    20. }
    21. if (!in.seek(pos)) QMessageBox::warning(this, "error", "Could not seek in textstream");
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    As the program runs, I keep getting the QMessageBox::warning because QTextStream::seek() is returning false. I've tried changing all instances from QTextStream::seek()to QFile::seek() - i.e., instead of in.seek(pos) I tried file.seek(pos), but it basically doesn't do anything and I wind up missing a Heading line. I know that the first heading section is being found and the sub lines are being processed because of the debug output. So the program reads/processes the first set of sub heading lines, reads in the next "Heading 2" line and breaks out of the inner while loop, but is still sitting on that 2nd heading line. So I wanted to seek back a line or two so that it would basically 'ignore' up to "Heading 2" and then process that section, and so on....

    Does anybody have any idea why the QTextStream::pos() and QTextStream::seek() combo is not working? Am I just approaching this completely wrong?

    Thanks,
    --Derrick

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextStream::seek() not working

    I would be inclined to adjust things a little and dispense with seek():
    Qt Code:
    1. if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    2. QTextStream in(&file);
    3. if (!in.atEnd()) {
    4. str = in.readLine();
    5. do {
    6. if (str.section(" ",0,0) == "Heading") {
    7. qDebug()<< "+" << str;
    8. str = in.readLine();
    9. while (!in.atEnd() && str.section(" ",0,0) == "") {
    10. //process sub head line
    11. qDebug()<< "*" << str;
    12. str = in.readLine();
    13. }
    14. }
    15. else
    16. str = in.readLine();
    17. } while (!in.atEnd());
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 
    You will need to check for correct behaviour with your actual data, but for this input:
    Qt Code:
    1. Line1
    2. Line2
    3. Heading 1
    4. sub head 1
    5. sub head 2
    6. sub head 3
    7. Heading 2
    8. sub head 1
    9. sub head 2
    10. Heading 3
    11. sub head 1
    12. ...
    13. Heading 4
    14. Heading 5
    15. more to ignore
    To copy to clipboard, switch view to plain text mode 
    it gives:
    Qt Code:
    1. + "Heading 1"
    2. * " sub head 1"
    3. * " sub head 2"
    4. * " sub head 3"
    5. + "Heading 2"
    6. * " sub head 1"
    7. * " sub head 2"
    8. + "Heading 3"
    9. * " sub head 1"
    10. + "Heading 4"
    11. + "Heading 5"
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChrisW67 for this useful post:

    derrickbj (6th November 2013)

  4. #3
    Join Date
    Sep 2006
    Posts
    38
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QTextStream::seek() not working

    Thank you for this. I want the same route after posting my comment to get things working and removed seek(). My was was a lot messier and involved some bools to flag when the sub heads were processed. Your way is a lot cleaner and works really well.

Similar Threads

  1. Replies: 8
    Last Post: 17th May 2012, 11:09
  2. QFile::Seek() not working
    By derrickbj in forum Qt Programming
    Replies: 2
    Last Post: 5th October 2011, 20:14
  3. sqlite seek() bug?
    By ibergmark in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2008, 07:44
  4. QFile, QTextStream, seek()
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 29th September 2006, 15:07

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.