[Qt-interest] Reading raw binary data

Arnold Krille arnold at arnoldarts.de
Wed Jun 10 09:37:46 CEST 2009


On Wednesday 10 June 2009 00:34:32 Anatoly Burakov wrote:
> 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
>
> QT help suggests using QDataStream for this to work. So i try and use it:
> 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?)

The reason is most probably that QDataStream aligns the data in its own kind 
of way. But a QFile is a QIODevice and these also allow direct reading and 
writing of bytes.
I noticed this when I was trying to read char* from a file with QDataStream, 
but had written these char* with QFile::write(). There are aligned as short as 
possible in the file (hexdump shows it), but QDataStream expects them to be 
somewhere starting at byte three or more...

Arnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090610/766fbaf4/attachment.bin 


More information about the Qt-interest-old mailing list