[Solved] Creating binary file in Qt?



  • Hi,
    I want to store a 2D array of double variables (double **myArray) in a binary file.
    I have not encountered binary files prior to this, even before Qt.
    So can you please help me by telling me the correct way to save the data to a binary file & again retrieve from it & put it back to the 2D array.
    Currently I am using text file, but for size > 1000x1000 the file size is going beyond what I want. Plus I have read that I/O from a binary file is substantially faster than text file.
    So can you please show me some code or atleast tell me the classes that I have to use?


  • Moderators

    read "this":http://qt-project.org/doc/qt-4.8/datastreamformat.html.

    basically you use the "<<" stream operator to write to a "QDataStream":http://qt-project.org/doc/qt-4.8/qdatastream.html#details, which writes to a QByteArray or directly to a QFile.



  • Hi,
    In addition to previous reply, if you plan to use your binary file on different platforms you may want to check that you always use the same endianess.
    i.e: always write and read big-endian (or little-endian) formatted data.
    By default QDataStream uses big-endian byte order so if you always use QDataStream AND default byte order you should be fine but if you try to read without QDataStream or with different byte order you may have wrong data.



  • Hi raven & unai_i
    Thanks for your advice.
    I now am able to add my data in a binary file.
    But somehow I am confused by the benchmarking I have done.
    I simply added lines of QString both with QDataStream & with QTextStream.
    I found strange results!
    The size of file created by QDataStream is substantially larger than that by QTextStream.
    Also QTextStream is faster than QDataStream but the difference is somewhat low.
    I was initially expecting the size of QDataStream file would be tremendously low than QTextStream.
    Also I was expecting faster I/O from QDataStream.

    PS: I used all the default settings for both streams.



  • Hi,
    If you use text "QString" it's better you use QTextStream than QDataStream since the taller will add some headers to indicate the type of data stored and size of data. Your question was about binary files. Binary files can store all kinds of data including text. For your "double" values you need to pass them directly to QDataStream without the QString intermediate conversion if you want them stored in binary format.


  • Moderators

    It also depends what actual values you are storing. For example an int of value 2 written using QTextStream will write a single byte 0x50 (a character in ASCII encodeing). The same int written with QDateStream will write 4 bytes(usually) that are the binary representation of that int. For doubles it's 8 bytes so you can see binary can be actually larger for some values. The size of specific types can also vary depending on what platform are you on.

    When using binary format make sure to clearly document what sizes and byte order you use and stick to it. Some formats for example declare it explicitly in the file header with a "BOM":http://en.wikipedia.org/wiki/Byte_order_mark



  • @Chris Thanks for that information about memory sizes!
    I think all the above answers solve all my questions.
    Thanks all!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.