PDA

View Full Version : Help with Binary reader in qt ???



rajji_saini
14th September 2010, 03:24
Hi,

I need help with reading binary files in Qt.
Following is something in dotnet and I need to implement similar stuff using Qt
----------------------------------------------------------------------------

{
position = 0;
byte[] buffer = new byte[sizeof(Int64)];
BinaryReader br = new BinaryReader(pStream);
br.Read(buffer, 0, recordSize);
position = BitConverter.ToInt64(buffer, 0);
return true;
}

------------------------------------------------------------------------------

I would really appreciate if you could please guide me to what is it that can be used in Qt to implement it ?

Regards,
Raj

tbscope
14th September 2010, 06:39
http://doc.qt.nokia.com/4.6/qdatastream.html

Something like this maybe?


QFile file(...);
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
while (!in.atEnd()) {
qint64 a;
in >> a;
// Do something with a
}

wysota
14th September 2010, 17:38
I think it is more like this:


QFile pStream(...);
pStream.open(...);
QByteArray buffer = pStream.read(sizeof(qint64));
qint64 val;
memcpy(&val, buffer.constData(), sizeOf(qint64));
Just note this is extremly unportable (which is not the case for the solution suggested by my preposter).

rajji_saini
15th September 2010, 20:35
Thanks guyz this helped me alot

rajji_saini
18th September 2010, 03:18
Hi,

I have a text file ... and I stored some value like "123" in it.
I am now trying to read the 3 bytes out of this text file as follows:

------------
QFile fileIn("Test.txt);
QByteArray buffer = fileIn.read(3);
qint32 val;
memcpy(&val,buffer.constData(),3);
qDebug() << "VAL:" << val;
qDebug() << "FileSize:" << fileIn->size();
------------
But I am not getting the outpt as 123 ... It gives some garbage value

FileSize: 3
VAL: 3355185

Could you please help ... what is it that I am doing wrong here ???

regards,
Raj

tbscope
18th September 2010, 07:29
The value 3355185 is not garbage, that is the correct number!

This is what happens:
You have a byte array containing the bytes (in decimal numbers): 49, 50, 51
The byte 49 represents the number 1, etc...

The same bytes in bits are:



Number | Byte | Bits
-------+-------+-------------------------
1 | 49 | 00110001
2 | 50 | 00110010
3 | 51 | 00110011


When you do the memory copy to an int, it will copy the 3 8-bit series into your 32 bit number, resulting in the number:

1100110011001000110001
Which is the number:
3355185


Do not make it yourself too difficult and use a QTextStream instead of a memory copy.


QTextStream stream(&inFile);

qint32 number = 0;

stream >> number;

qDebug() << "number:" << number;

rajji_saini
20th September 2010, 04:02
Can I use QTextStream in a scenario where I have to read a specific number of bytes from the given data.
In my example above I mentioned a text file ... How can I implement if I have to lets say read first two bytes out of a binary file ???
Considering the same example above if I were to read only "1" and store it as an int ... How could I do that ???

Regards,
Raj

tbscope
20th September 2010, 06:34
Consider the following file:


This is a text containing the number 1 2 and 1234

You can use something like this:


QTextStream stream(&inFile);

QString text = QString("");
qint32 number = 0;
char *bigNumber = "";

stream >> text; // text contains the word "This"
stream >> text; // text contains the word "is"
stream >> text; // text contains the word "a"
stream >> text; // text contains the word "text"
stream >> text; // text contains the word "containing"
stream >> text; // text contains the word "the"
stream >> text; // text contains the word "number"
stream >> number; // number = 1
stream >> text; // text contains the word "2"
stream >> text; // text contains the word "and"
stream >> bigNumber; // bigNumber = "1234", bigNumber[0] = "1", etc...
When using bigNumber and a text stream, make sure you double check the sizes!!!