Results 1 to 4 of 4

Thread: Output a QVector of doubles to a binary file (without using QDatastream)

  1. #1
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    39
    Thanks
    15
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Output a QVector of doubles to a binary file (without using QDatastream)

    okay so i once (a long time ago) had some code I wrote that let me write binary output files but I have lost it and cant remember how to get it working. After much trawling I am now asking for help.

    So if i have a QVector<double> how can I write this to a binary file?

    I know I need QFile and QByteArray (and possibly reinterpret_cast) and size_of(double) but I don't know how to put it together properly. Its driving me nuts and I am sure its probably rather simple.

    Cheers

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Output a QVector of doubles to a binary file (without using QDatastream)

    If you do not need to worry about byte order then something like:
    Qt Code:
    1. QVector<double> vec(1000, 0.0);
    2. QFile file("output.bin");
    3. if (file.open(QIODevice::WriteOnly) {
    4. qint64 bytesWritten = file.write(static_cast<const char*>(vec.constData()), sizeof(double) * vec.size());
    5. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    39
    Thanks
    15
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Output a QVector of doubles to a binary file (without using QDatastream)

    Hi Chris,

    you've come to my rescue once again. I still use the code snippets you provided for reading binary data

    Am I right in assuming that the byte order problem is only if i go from a PC to unix (little to big) but if i am staying in the little endian environment then its not a problem? I wont be swapping environments for what its worth.

    Cheers
    Andrew


    Added after 19 minutes:


    Hi again Chris,

    i got the following on compilation
    error: invalid static_cast from type 'const double*' to type 'const char*'
    qint64 bytesWritten = outputFile.write(static_cast<const char*>(atmosParams.constData()), sizeof(double)*atmosParams.size());

    atmosParams is a QVector equivalent to the vec QVector used in the example snippet. Had a quick flick around and changed static_cast to reinterpret_cast and all worked.

    Due to my lack of knowledge here I presume its probably all okay
    ^
    Last edited by OzQTNoob; 8th April 2016 at 02:47.

  4. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Output a QVector of doubles to a binary file (without using QDatastream)

    (1) Endianity relates to CPUs, not to operating systems. It does not matter whether PC or Unix, it matters whether, for example, Intel or Motorola.
    (2) Yes, reinterpret cast, not static cast. reinterpret cast simply changes the pointer meaning (from const double * to const char *) without bothering of the actual data at the pointer (and that's why it is considered dangerous but it is okay here). static cast tries to convert.

Similar Threads

  1. reading doubles from binary file
    By shivendra46d in forum Newbie
    Replies: 1
    Last Post: 21st January 2016, 09:54
  2. Replies: 4
    Last Post: 25th December 2014, 09:09
  3. Replies: 4
    Last Post: 4th November 2014, 22:53
  4. Replies: 10
    Last Post: 9th February 2012, 05:31
  5. Replies: 6
    Last Post: 24th November 2010, 18:59

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.