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

william.crocker at analog.com william.crocker at analog.com
Mon Mar 7 15:23:17 CET 2016


On 03/07/2016 09:19 AM, Sina Dogru wrote:
>
>
> 2016-03-07 15:51 GMT+02:00 william.crocker at analog.com
> <mailto:william.crocker at analog.com> <william.crocker at analog.com
> <mailto: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?
>

You did not address my other two observations.

    > You are reading an 'int' but you did not insure that an 'int' was written.
    > You wrote a QByteArray, but are then reading a QString.

> 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



More information about the Interest mailing list