PDA

View Full Version : Missng last lines when read file wth readLine()



aguayro
19th August 2012, 21:17
Hi, i'm trying to read a entire file:



QFile lista(path);
if(!lista.open(QFile::ReadOnly))
{
QMessageBox msgBox;
msgBox.setText(tr("Could not open file.(2)"));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec();
} else {
QTextStream strim(&lista);

while(!lista.atEnd()) {
qDebug() << strim.readLine();
}
}
lista.close();

But i can't read the last lines, it never read the last lines of files, sometimes 50, sometimes 100,.....

What can be wrong??

ChrisW67
20th August 2012, 03:34
What can be wrong??

Almost anything:

You could be reading a file other than the one you think you are.
You could be reading a file that is being appended to by another process, so by the time you check the file is has more content.
You could be reading a binary file where the concept of "line" in meaningless.
You could could be reading a file with old-style Mac OS line endings expecting these to be treated as line endings.
You could just be mistaken about what is actually read.

Rhayader
20th August 2012, 08:07
from QTextStream documentation:

Since the text stream uses a buffer, you should not read from the stream using the implementation of a superclass. For instance, if you have a QFile and read from it directly using QFile::readLine() instead of using the stream, the text stream's internal position will be out of sync with the file's position.

So maybe you should use " while (!strim.atEnd()) " to check the end of file

aguayro
20th August 2012, 14:07
thanks, its working fine now :)