Results 1 to 7 of 7

Thread: QDataStream vs. fwrite

  1. #1
    Join Date
    Jan 2007
    Location
    San Diego, California, USA
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QDataStream vs. fwrite

    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.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QDataStream vs. fwrite

    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream vs. fwrite

    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.

  4. #4
    Join Date
    Jan 2007
    Location
    San Diego, California, USA
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream vs. fwrite

    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?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataStream vs. fwrite

    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".

  6. #6
    Join Date
    Jan 2007
    Location
    San Diego, California, USA
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream vs. fwrite

    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataStream vs. fwrite

    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.

Similar Threads

  1. QDataStream reading into QString
    By jakamph in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2006, 09:22
  2. QDataStream
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 23rd August 2006, 14:40
  3. avaiability of data from QDataStream
    By quickNitin in forum Newbie
    Replies: 1
    Last Post: 19th June 2006, 12:32
  4. Reading QByteArray from QDataStream Qt3.3
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 20:23
  5. QDataStream >> QString
    By smalls in forum Qt Programming
    Replies: 2
    Last Post: 17th January 2006, 22:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.