[Qt-interest] Reading raw binary data

Anatoly Burakov burakov.anatoly at googlemail.com
Wed Jun 10 00:34:32 CEST 2009


Hello everyone!

I am porting old custom-written DOS accounting app (Borland C++ 1.3 or
so) to QT4. This program reads and writes binary files. I need to read a
float value from one of the binary files (probably other data types too
but i think if i figure out this one - others would be similar). I
clearly see that there is a float value on the offset 0x0c (12) in the
hex editor and i also have a BC++ source code which reads float value
with the following code:

// public variables

float dollar_price=0;

// main

// ... blablabla main
int handle=open("FILENAME",O_BINARY|O_RDONLY);
// ... three 4-byte reads make up offset 12
_read(handle,&dollar_price,4);
// ... blablabla
close(handle);

QT help suggests using QDataStream for this to work. So i try and use it:

QFile file("FILENAME");
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
file.seek(12); // getting to offset 12
float usd_price;
in >> usd_price;	// reading serialized value, according to QT4 help system
file.close();
QVariant str = usd_price; // poor man's typecasting i guess
QMessageBox mb;		// dummy message to see if the value is right
mb.setText(str.toString());
mb.exec();

The problem is - QDataStream reads rubbish. I know what should be there
and i see that this is not what QDataStream reads. Any ideas why is this
happening? Or am i missing something fundamental here? (maybe it's the
typecasting to blame? is there a way to read directly to float value,
without using QVariant?)

Thank you,
Anatoly



More information about the Qt-interest-old mailing list