Results 1 to 11 of 11

Thread: reading a binary file full of floats and/or doubles

Threaded View

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

    Default reading a binary file full of floats and/or doubles

    Okay so I have been trying to read in a binary file that is full of floats (little endian, single precision) and have managed to do that with QDataStream (see code below). The problem is though that these files are of the order of 531x50000 floats, and where each of the 531 represents a spectrum. If i loop over the whole file with 2 for loops (e.g. for t=0,530 and for i=0,49999) then it is really quite slow, the whole file is only 54MB. And as far as i can tell I can only read in one float at a time. Ideally I dont want to read the entire file (sometimes I might though), but I would prefer to read in an entire spectrum in one go e.g. all 531 float values at once rather than using nested loops. Can anyone point me in the right direction?

    Qt Code:
    1. qint64 pos( 0);
    2. float bob;
    3. QVector<float> vect(531);
    4. bool ok = 1;
    5. .......
    6.  
    7. QFile file(fileName);
    8. file.open(QIODevice::ReadOnly );
    9. QDataStream stream( &file );
    10. stream.setByteOrder(QDataStream::LittleEndian);
    11. stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
    12. stream.device()->reset();
    13. for(int i=0;i<531;i++){
    14. stream.device()->seek( pos );
    15. stream >> bob;
    16. vect[i] = bob;
    17. pos +=4;
    18. stream.device()->reset();
    19. }
    To copy to clipboard, switch view to plain text mode 

    I also tried the above using readRawData but couldnt get it to work at all as the toFloat function always returns FALSE. I tried this to see if I could get it to work so I could then make my datain array have a size of 4x531 and just read in a whole spectrum in one go. What did I do wrong. I am a complete noob as well.

    Qt Code:
    1. qint64 pos( 0);
    2. QByteArray datain;
    3. datain.resize(4);
    4. float bob2;
    5. bool ok = 1;
    6.  
    7. ...
    8. QFile file(fileName);
    9. file.open(QIODevice::ReadOnly );
    10. QDataStream stream( &file );
    11. stream.setByteOrder(QDataStream::LittleEndian);
    12. stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
    13. stream.device()->reset();
    14. for(int i=0;i<531;i++){
    15. stream.device()->seek( pos );
    16. stream.readRawData( datain.data(), datain.size() );
    17. bob2 = datain.toFloat(&ok); <- THIS ALWAYS RETURNS A FALSE VALUE :(
    18. pos +=4;
    19. stream.device()->reset();
    20. }
    To copy to clipboard, switch view to plain text mode 


    I should add that I have got the seek(pos) option in there because I was playing around to see what it was reading. If I read my data in from different parts of the file then I will have to use seek.
    Cheers
    Oz
    Last edited by OzQTNoob; 8th February 2012 at 09:48.

Similar Threads

  1. Replies: 4
    Last Post: 4th November 2014, 22:53
  2. Replies: 2
    Last Post: 17th May 2011, 10:47
  3. Binary file reading using Structure
    By umulingu in forum Qt Programming
    Replies: 6
    Last Post: 25th July 2009, 11:35
  4. Reading characters in Binary file.....
    By umulingu in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2009, 04:51
  5. Binary file Reading.........
    By umulingu in forum Qt Programming
    Replies: 11
    Last Post: 20th July 2009, 06:18

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.