[Solved]QFile $ QStream What is that?
-
hi , is that a bug or something i can't understand
@#include <QtCore>int main()
{
QFile File("test.txt");
File.open(QIODevice::ReadWrite | QIODevice::Text);QTextStream out(&File); out << "QTextStream , First Line \n"; File.write("QFile , Second Line \n"); File.close();
}@
the output file
test.txt is
QFile , Second Line
QTextStream , First LineI have more bugs with QTextStream or we must use one method QFile or QTextStream ?
I write this code under Mac OSX ,Not tested on windows -
Well,
if you mix up QFile write calls and QTextStream write calls, I think that you can encounter some strange output due to the fact that (I suppose) QTextStream "buffers" its data for a while before writing for real.
So I suggest to write your file using "only" QTextStream or "only" QFile write calls.
Tony.
-
From "QTextStream Class Reference":http://doc.trolltech.com/4.7/qtextstream.html:
bq. Internally, QTextStream uses a Unicode based buffer, ...
bq. 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 the conclusion is, as mentioned by Antonio: you must not mix up using methods of QTextStream and QFile on the very same file.
-
- As Antonio mentioned you should use only one approach and don't mix direct file writing and text stream writing.
- By declaring your text stream you say that everything will go to file, but not as soon as you use << operator. When something is outputted via << operator it sits in a intermediate buffer and waits for flushing. So, you need to call
@out.flush();@
and this will put data from buffer into a file that your stream is bound to.
-
@out.flush();@
thanks I think i understand now