PDA

View Full Version : Strange binary file output when using QByteArray.



schunyeh
24th December 2014, 00:30
Hi, all,

I have a problem of QByteArry.
When I take it to store a data, a strange binary file is outputted.
A basic code is like as following.


QString fname ="test1.db";
QFile ext_file(fname);
ext_file.open(QIODevice::WriteOnly);
QDataStream out(&ext_file);
static const char mydata[] = { 0x74,0x68,0x65 };
QByteArray data = QByteArray::fromRawData(mydata,sizeof(mydata));
out<<data;


However, I got a strange extra data on the head of data stream.



0000 0003 7468 65


Why does the output data have 0000 0003 ?
Could you please help me.
Thank you very much.

wysota
24th December 2014, 01:22
QDataStream is a serialization mechanism and not a general purpose binary stream. It prepended the length of the byte array to the stream to be able to deserialize it later.

anda_skoa
24th December 2014, 11:13
Just to emphasis what wysota wrote: you problem has nothing to do with QByteArray.
Write it directly into the file instead of serializing it as an object.

Cheers,
_

schunyeh
25th December 2014, 00:27
Thank you, anda_skoa and wysota.
I will try to use std::fstream or fwrite.

wysota
25th December 2014, 09:09
It is enough to use QFile::write.