I have these 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 a text file for a specific char sequence, and when found, load the file from the beginning to the occurrence of the searched text. 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 comes the strange part - if the searched string is on any of lines save for the last, I get the expected behavior. It works perfectly fine.
BUT if I search for a string that is on the last line 6, the result is always 5 characters short. If it was the 7th line, the result would be 6 characters short and so on, when the searched string is on the last line, the result is always lineNumber - 1
characters shorter.
So, is this a bug or I am missing something obvious?
EDIT: Just to clarify, I am not asking for alternative ways to do this, I am asking why do I get this behavior.