I have a few lines of code:
QFile file("h:/test.txt"); file.open(QFile::ReadOnly | QFile::Text); QTextStream in(&file); bool found = false; uint pos = 0; do { QString temp = in.readLine(); int p = temp.indexOf("something"); if (p < 0) { pos += temp.length() + 1; } else { pos += p; found = true; } } while (!found && !in.atEnd()); in.seek(0); QString text = in.read(pos); cout << text.toStdString() << endl;
The idea is to search for a text file for a specific char sequence, and if it is found, load the file from the very beginning until the text you are looking for appears. The input I used for testing was:
this is line one, the first line this is line two, it is second this is the third line and this is line 4 line 5 goes here and finally, there is line number 6
And here's the weird part - if the search string is in any of the strings saved for the last, I get the expected behavior. It works great.
BUT , if I look for the line that is on the last line of 6, the result will always contain 5 characters. If it was the 7th line, the result will be 6 characters, etc., When the desired line is in the last line, the result is always lineNumber - 1 characters shorter.
So, is this a mistake, or am I missing something obvious?
EDIT: just to clarify, I am not asking for alternative ways to do this, I ask why I get this behavior.
dtech
source share