I have this basic program:
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 input file looks like this:
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
The idea is of course, to find the first occurrence of a string and load the text file from start to that location. Passing strings that are on the first 5 lines results in the expected output:
with indexOf(“first”) output is:
this is line one, the
with “cond”:
this is line one, the first line
this is line two, it is se
with “here”:
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
However, if I pass “num” that is on the last line I get an unexpected result:
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
There are 5 symbols missing on line 6, if it was line 7 there would be 6 symbols missing and so on, all the lines but the last behave normally, the last line cuts lineNumber – 1 symbols.
Maybe it’s because its 5 AM, but I’ve been starring at this for line 30 minutes and cannot figure out why… so humiliating…
↧