PDA

View Full Version : QDataStream vs. fwrite



s43lee
24th January 2007, 16:19
Hi --

I just started playing around with Qt (v. 4.2) on Linux, and I'm a little confused on the output data format used in writing binary float or double with QDataStream. How does this format compare with binary writes using fwrite()? -- the binary output results I get from both are different.

thanks.

jpn
24th January 2007, 16:39
Format of the QDataStream Operators (http://doc.trolltech.com/4.2/datastreamformat.html)

e8johan
24th January 2007, 16:43
Qt ensures that the format can be used across the platforms supported. This means that what you write with a QDataStream always can be read - regardless of where it was written or read. One consequence of this can be that it outputs something that is different compared to fwrite.

s43lee
24th January 2007, 16:56
Yes, I know about all the data stream formats but my point was if I write:

QFile ofile("output.dat");
ofile.open(QIODevice::WriteOnly);
QDataStream out(&ofile);
float a=3.2;
out<<a;

The result I get is different than if I had done the binary float write with fwrite() using gcc on Linux. So my question is what is there a difference in how QDataStream and fwrite() are interpreting float for binary output? I know in the documentation, it says that QDataStream uses IEEE 754 format. Does fwrite use a different format? Is the byte ordering different?

wysota
24th January 2007, 21:20
Please read the above answers again. They contain what you ask for. Just a hint - fwrite() stores values in a platform-dependent way, whereas QDataStream does that in platform independent way. The answer should get even more clear when I mention phrases like "big-endianness" and "little-endianness".

s43lee
26th January 2007, 07:51
Sorry -- I wrote my 2nd post before I had a chance to read the note from e8Johan. Yes, it complete sense. So I guess if you want the convenience of avoiding inter-platform byte swapping, you have to accept the risk of conflicting with the format used by most other applications in any given platform -- and unfortunately this conflict seems to be the case in Linux.

wysota
26th January 2007, 10:12
What conflict? Who forbids you to use QFile and its write() method directly? And there are always regular C++ streams if you need streamed access and you can even use QDataStream with raw access to write "simple" data. QDataStream is to be used mainly for serialisation, not general stream access.