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....???