Results 1 to 3 of 3

Thread: QFile::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 QFile::Seek() not working

    Hello,

    I may not be doing something correctly, but can't see any other posts related to my issue. I'm reading in a file and searching for specific text. When Text is found, I break out of my while (!atEnd()) loop and change the position back to 0 by QFile::seek(0). It doesn't appear to work. Moreover, I had to do some odd behavior in order to make it work:

    Code that doesn't work:

    Qt Code:
    1. QFile file(myFile)
    2. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
    3. QTextStream in(&file)
    4. while (!(in.atEnd())) {
    5. QString str = in.readLine();
    6. //...some other code....
    7. if (str == text_to_find) break;
    8.  
    9. }
    10. if (!file.seek(0)) return;
    11.  
    12. //loop through map and find each item in file, displaying which ones exist
    13. int i;
    14. for (i=itemsMap.begin(); i != itemsMap.end(); ++i) {
    15. QString cmp = i.value();
    16. while (!file.atEnd()) {
    17. str = in.readLine();
    18. if (str == cmp) std::cout << "comparison found";
    19. }
    20. if (!file.seek(0)) return;
    21. }
    To copy to clipboard, switch view to plain text mode 

    Code that works:


    Qt Code:
    1. QFile file(myFile)
    2. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
    3. QTextStream in(&file)
    4. while (!(in.atEnd())) {
    5. QString str = in.readLine();
    6. //...some other code....
    7. }
    8. if (!file.seek(0)) return;
    9. str = in.readLine()
    10. if (!file.seek(0)) return;
    11.  
    12. //loop through map and find each item in file, displaying which ones exist
    13. int i;
    14. for (i=itemsMap.begin(); i != itemsMap.end(); ++i) {
    15. QString cmp = i.value();
    16. while (!file.atEnd()) {
    17. str = in.readLine();
    18. if (str == cmp) std::cout << "comparison found";
    19. }
    20. if (!file.seek(0)) return;
    21. }
    To copy to clipboard, switch view to plain text mode 

    For some reason, if I put the break statement back in the first while loop, the 2nd while loop (within the first iteration through the for loop) picks up where the previous while loop left off. If I comment out the break, as in the second code snippet, it still doesn't seem to do the job. After the first while loop, I have to do a seek(0), then read in a line, then do another seek(0). When debugging, it seems that it treats the first line in the file as the last line of the file....???

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QFile::Seek() not working

    You are operating on a stream not the file, so try:
    Qt Code:
    1. in.seek(0)
    To copy to clipboard, switch view to plain text mode 
    Also line 16 should be:
    Qt Code:
    1. while (!in.atEnd())
    To copy to clipboard, switch view to plain text mode 

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

    derrickbj (5th October 2011)

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

    Default Re: QFile::Seek() not working

    That did the trick, thanks so much! This was driving me nuts

Similar Threads

  1. Phonon+seek
    By Fastman in forum Qt Programming
    Replies: 8
    Last Post: 8th September 2010, 10:31
  2. How to use the QFile::seek() in Qt-4.4.3
    By grsandeep85 in forum Qt Programming
    Replies: 2
    Last Post: 3rd April 2010, 11:54
  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.