[Interest] reading from and writing to a file with QDataStream

Sina Dogru sinadooru at gmail.com
Mon Mar 7 15:19:26 CET 2016


2016-03-07 15:51 GMT+02:00 william.crocker at analog.com <
william.crocker at analog.com>:

> Some tool kits use a buffer in their version of QDataStream.
> You can not in general be sure that anything has actually been
> written to 'bai' here because 'out' is still alive.
>

So I would be sure if I write it in a block I guess?

int main(int argc, char *argv[])
{
    QFile file("file.dat");
    {
        // module A creates a byte array
        QByteArray bai;
        QDataStream out(&bai, QIODevice::WriteOnly);
        out << QString("A QString"); // writes some information on it

        // passing bai to module A
        // module A do something...


        file.open(QIODevice::WriteOnly);
        QDataStream out2(&file); // and module A write those datas to a
file.
        out2 << bai.size();
        out2 << bai;
    }

    // and module B starts
    if (file.isOpen())
        file.close();

    file.open(QIODevice::ReadOnly);
    QDataStream in(&file);

    QString str;
    int a;                  // extracts size information
    in >> a >> str;         // extracts QString

    qDebug() << str << a; // output: "" 22
    return 0;
}

But it also gives the same output. Or am I misunderstood?

Thank you
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160307/c6c05ccb/attachment.html>


More information about the Interest mailing list