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

André Somers andre at familiesomers.nl
Tue Mar 8 08:25:14 CET 2016



Op 07/03/2016 om 19:00 schreef william.crocker at analog.com:
>
>>>
>>>      QDataStream out2(&file); // and module A write those datas to a 
>>> file.
>>>      out2 <<  bai.size();
>>>      out2 <<  bai;
>>
>
> All of the following is IMHO:
>
> I think that if you are going to read the size back as an 'int'
> you should insure that an 'int' is written in the first place:
>
>     out2 << (int)bai.size();
>
> Sure, you can look and see that the size() of a QByteArray returns
> an 'int', but that is only as of today. You should get into the habit
> of controlling the types written to QDataStreams.
Actually, for reading and writing binary data files, I think it is wise 
to get into the habbit of using explicitly sized types. That is, write 
out and read back a qint8, a qint32 or a qint64, not an int or a long. 
int and other integer types are not guaranteed to be the same size 
across platforms, so you may get into trouble if you are not explicit 
about how many bytes you want or expect your values to be encoded in.

André




More information about the Interest mailing list