PDA

View Full Version : Problems reading binary data from file using QDataStream



gowen
22nd November 2010, 22:16
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:




SomeStructure *someStruct;

QFile file("somedata.raw");
file.open(QIODevice::ReadOnly);
QDataStream fileStream(&file);

fileStream.readRawData(reinterpret_cast<char*>&someStruct, someByteSize);



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

Zlatomir
22nd November 2010, 22:44
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)

ChrisW67
22nd November 2010, 23:05
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.

gowen
22nd November 2010, 23:58
Thank you for the quick replies!

I have initialised the pointers using:


someStructure *someStruct = new someStructure();

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

ChrisW67
23rd November 2010, 00:47
QFile is a QIODevice. Something like:


someStructure *someStruct = new someStructure();
QFile file("somedata.raw");
if (file.open(QIODevice::ReadOnly))
{
qint64 bytes = file.read(static_cast<char *>(someStruct), sizeof(someStructure));
if (bytes == sizeof(someStructure)) {
// read was good
}
}

faldzip
23rd November 2010, 09:21
&someStruct
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.

gowen
24th November 2010, 19:59
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!