Results 1 to 7 of 7

Thread: Problems reading binary data from file using QDataStream

  1. #1
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Problems reading binary data from file using QDataStream

    Hi All,

    I am very new to Qt and C++ programming in general and have got into some difficulty.

    I am trying to read data from a very large binary file (upto 1GB in size) but I keep having problems.

    I would like to read a certain number of bytes into a struct using QDataStream. I can compile and run the code with no errors, but it is not reading in the data that I expect.

    Here is a simplified version of the code:

    Qt Code:
    1. SomeStructure *someStruct;
    2.  
    3. QFile file("somedata.raw");
    4. file.open(QIODevice::ReadOnly);
    5. QDataStream fileStream(&file);
    6.  
    7. fileStream.readRawData(reinterpret_cast<char*>&someStruct, someByteSize);
    To copy to clipboard, switch view to plain text mode 

    I cannot read all the data in at once due to the size of file and would like the ability to move through the data that QDataStream gives.

    I can get this to work using the ifstream, however this appears to be causing a memory issue a few lines later that I cannot correct. Therefore I thought it better to try the Qt functions for file reading.

    If anyone can help then I would be so grateful! as this issue has been driving me mad!

    Many Thanks

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problems reading binary data from file using QDataStream

    The problem is this line SomeStructure *someStruct;, here you only declare a pointer which points to nowhere in the memory, since it's not initialized. (create an object, or use new with the pointer, and if you use new, don't forget to delete if it is not part of a parent-child relationship)

  3. The following user says thank you to Zlatomir for this useful post:

    gowen (22nd November 2010)

  4. #3
    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: Problems reading binary data from file using QDataStream

    Unless the creator of your struct is a Qt application then you probably want the QIODevice read methods rather than QDataStream. QDataStream is designed for platform agnostic serialisation and recovery of intrinsic and Qt data types. It stores extra information and potentially different byte-orders to an 'equivalent' plain-old-data structure.

  5. The following user says thank you to ChrisW67 for this useful post:

    gowen (22nd November 2010)

  6. #4
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems reading binary data from file using QDataStream

    Thank you for the quick replies!

    I have initialised the pointers using:

    Qt Code:
    1. someStructure *someStruct = new someStructure();
    To copy to clipboard, switch view to plain text mode 

    but this still didn't seem to work...

    I have tried playing around with the QIODevice read, but I do not think I have quite got it right.... how would I initialise the device to read from a file? and would the code be the same to read? Other than changing the function?

    Thanks

  7. #5
    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: Problems reading binary data from file using QDataStream

    QFile is a QIODevice. Something like:
    Qt Code:
    1. someStructure *someStruct = new someStructure();
    2. QFile file("somedata.raw");
    3. if (file.open(QIODevice::ReadOnly))
    4. {
    5. qint64 bytes = file.read(static_cast<char *>(someStruct), sizeof(someStructure));
    6. if (bytes == sizeof(someStructure)) {
    7. // read was good
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to ChrisW67 for this useful post:

    gowen (24th November 2010)

  9. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problems reading binary data from file using QDataStream

    Quote Originally Posted by gowen View Post
    Qt Code:
    1. &someStruct
    To copy to clipboard, switch view to plain text mode 
    This gives you a type SomeStructure ** - I think you want a SomeStructure *?

    QDataStream, as mentioned, is a serialization mechanism so in simple words - it can read files written with QDataStream.
    Is your "samedata.raw" written with QDataStream? If not - you QIODevice functionality as shown above.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  10. #7
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems reading binary data from file using QDataStream

    Thanks to all! I have finally managed to read in the data correctly! I have also found out the error I was having later was due to a memory leak from reading data into a structure which was not big enough!

    Thank you again!

Similar Threads

  1. Reading/writing data to binary file
    By DiamonDogX in forum Qt Programming
    Replies: 3
    Last Post: 23rd July 2009, 19:24
  2. Reading characters in Binary file.....
    By umulingu in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2009, 04:51
  3. Binary file Reading.........
    By umulingu in forum Qt Programming
    Replies: 11
    Last Post: 20th July 2009, 06:18
  4. QFile, QDataStream reading binary data
    By yren in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2009, 06:34
  5. Reading binary data
    By vermarajeev in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2007, 09:14

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.