PDA

View Full Version : QTextStream + QFile issue



trueneo
22nd September 2007, 13:08
Hi ,
before sending this issue to Trolltech maybe some one of you, skilled people, can answer where I'm wrong:



//main.cpp

include <QtCore>
int main(int argc, char **argv)
{
QFile f("main.cpp");
f.open(QIODevice::ReadOnly);
QTextStream ts(&f);
QString s = ts.readLine();
qDebug("%s", s.toLatin1().data());
f.flush();
f.close();
f.setFileName("pippo.txt");
f.open(QIODevice::ReadOnly);
ts.flush(); //does nothing
ts.setDevice(&f); //AGAIN?
s = ts.readLine();
qDebug("This one will not work");
qDebug("First line: %s", s.toLatin1().data());
ts.seek(0);
s = ts.readLine();
qDebug("This one will work");
qDebug("First line: %s", s.toLatin1().data());

return 0;
}

//pippo.txt
** If you read this line the QTextStream class works **

ashukla
22nd September 2007, 14:13
What do u wants to do?

jpn
22nd September 2007, 14:45
QTextStream::flush() flushes write buffer so don't expect it to do anything on a read only buffer. But I think I can see your point. You're saying that QTextStream::setDevice() should reset text stream's internal position, right?

trueneo
22nd September 2007, 16:16
Ok seems that I do not explain the point, my fault.
If you run the prog you will notice that the out put will be this:

#include <QtCore>
This one will not work
First line: #include <QtCore>
This one will work
First line: ** If you read this line the QTextStream class works **

Closing the first file and opening another one does not affect QTextStream buffer until seek(0) is called.
If flush() is intended for write only, how could we delete the buffer when we open another file?
the answer seems to be "call seek(0)";
If you make a prog that ask for a file via QfileDialog, read the first line of the file using QTextStream:: readLine() and display it in a TextEdit. changing the file will not change the TextEdit contents because QTextStream::readLine() continue to read the same data.
QTextStream::readAll() is not affected.
Any comments are welcome.