Results 1 to 4 of 4

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

Hybrid View

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

    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
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    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 

Similar Threads

  1. reading doubles from binary file
    By shivendra46d in forum Newbie
    Replies: 1
    Last Post: 21st January 2016, 10:54
  2. Replies: 4
    Last Post: 25th December 2014, 10:09
  3. Replies: 4
    Last Post: 4th November 2014, 23:53
  4. Replies: 10
    Last Post: 9th February 2012, 06:31
  5. Replies: 6
    Last Post: 24th November 2010, 19: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
  •  
Qt is a trademark of The Qt Company.