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] Writing double to file using QDataStream?
Forum Update on Monday, May 27th 2025

[Solved] Writing double to file using QDataStream?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 4.1k 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.
  • C Offline
    C Offline
    CAD_coding
    wrote on 29 Dec 2013, 14:38 last edited by
    #1

    I have a 2D array of doubles which I would like to store to a file in binary format. I understand that QDataStream is the required class for that. This is my current code:

    @void FileString::SaveBinary()
    {
    if(dataStream)//QDataStream *dataStream
    {
    QString line = "MyApp 1.1.1.1";
    *dataStream << line << '\n';
    line.setNum(n);//int n
    *dataStream << line << '\n';

        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
                *dataStream << array1[i][j];//double **array1
    
            *dataStream << array2[i];//double *array2
            *dataStream << "\n";
        }
    
        line = "EOF";
        *dataStream << '\n' << line;
    }
    
    QThread *thr = QThread::currentThread();
    disconnect(thr, 0, this, 0);
    this->moveToThread(mainThread);//mainThread = GUI thread
    thr->exit(0);//
    

    }@

    SaveBinary is a SLOT run on a different thread than the GUI thread.
    I am concerned that if this file is opened on a different hardware then it may fail.
    So I would like to know what would be a better way to write double to a file and the file can be opened on any PC (irrespective of the processor architecture, compiler, etc).

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 29 Dec 2013, 16:13 last edited by
      #2

      From the QDataStream doc:
      "A data stream is a binary stream of encoded information which is 100% independent of the host computer's operating system, CPU or byte order. For example, a data stream that is written by a PC under Windows can be read by a Sun SPARC running Solaris."

      So you should be fine with doubles, but what bothers me more is that you say you want a binary file and yet you use platform dependant text to separate fields (the newline character and "EOF" string?). AFAIK binary formats don't usually use any field separators or markers like that. They rather keep offsets and sizes in some sort of data chunks.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CAD_coding
        wrote on 29 Dec 2013, 17:07 last edited by
        #3

        Hi Chris, regarding the newlines the code be modified so that QString is used:

        @line = "\n";
        *dataStream << line;@

        This can be replaced above instead of '\n' and "\n".

        But I do not understand your objection to "EOF"?
        I am using QString too write this.
        Then what is the problem here?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 29 Dec 2013, 17:22 last edited by
          #4

          There is no problem per se. I just think for a binary format (which is often chosen for compactness) it's a little wasteful.
          Why do you need \n at all? QDataStream encodes the length of the string and knows sizes of different types. You can write consecutive fields without separating them with anything. If you really need there something then \n is a bad choice since QDataStream will treat it as a usual QString, add a type id, length etc. There's also matter of \n being different on linux/windows.
          Similarly, what is the use of "EOF" sting at the end? Can't the stream just end?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            CAD_coding
            wrote on 30 Dec 2013, 14:47 last edited by
            #5

            OK, removed all the \n and EOF stuff!
            Thanks for your advice :)

            1 Reply Last reply
            0

            3/5

            29 Dec 2013, 17:07

            • Login

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