Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Creating binary file in Qt?

[Solved] Creating binary file in Qt?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 7.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CAD_codingC Offline
    CAD_codingC Offline
    CAD_coding
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • U Offline
        U Offline
        unai_i
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • CAD_codingC Offline
          CAD_codingC Offline
          CAD_coding
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • U Offline
            U Offline
            unai_i
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • CAD_codingC Offline
                CAD_codingC Offline
                CAD_coding
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved