qtqfileqtextstream

Writing Issues With QTextStream


QFile vfile(file);
if(!vfile.open(QIODevice::ReadWrite | QIODevice::Text)) qDebug() << "FILE COULDN NOT BE OPENED";
QTextStream stream(&vfile);

stream << "Hello" << "=";
vfile.write("132");

Output to File - 132Hello=

In the above example, I write the data in 2 different ways but when I see the file I found some this type of result that while using "write()" the data within write() printed first instead of the above statements is displayed in the example.


Solution

  • The stream data is cached for a time (which is typical of writing to streams in general, eg. stdout and such). You can flush the stream data to be sure it is all written before writing to the file via a different method.

    stream << "Hello=" << flush;
    vfile.write("123");
    

    Also see manipulator functions list in https://doc.qt.io/qt-5/qtextstream.html#details

    Writing an end-of-line character (endln or \n) will also flush the stream buffer.