PDA

View Full Version : QByteArray in Qt3



joseph
5th September 2007, 11:13
Hello All,
I have a code snippet written in Qt4. I want to port the following code to Qt3. I have given my attempt but the data what I get is "not complete". Since the data is binary, I assume a NULL character is encountered and terminates to read further.

Qt4 code


QFile fRead(inputFileName);
if (!fRead.open(QIODevice::ReadOnly))
return;

QByteArray message = fRead.readAll();
fRead.close();

QByteArray left16 = message.left( 16 ); //get first 16 bytes
QByteArray restRightAfter16 = message.right( message.length() - 16 );


Qt3 code


QFile fRead(inputFileName);
if (!fRead.open(IO_ReadOnly))
return;

QByteArray message = fRead.readAll();
fRead.close();

QCString strleft16 = QCString( message.data() );
QByteArray left16 = strSalt.left( 16 );

QCString strRestRightAfter16 = QCString( message.data() );
QByteArray restRightAfter16 = strRestRightAfter16.right( strBody.length() - 16 ); //I dont the full data into restRightAfter16. The data is incomplete.


The binary file is really large and I want to read the entire data as given in the code above. I dont have any problems with Qt4 code and it works very fine.
Where I'm I going wrong?

Thanks

null_pointer
6th September 2007, 06:16
Use QDataStream class.


The QDataStream class provides serialization of binary data to a QIODevice.

Example from Qt3 docs:


QFile file( "file.dat" );
file.open( IO_ReadOnly );
QDataStream stream( &file ); // read the data serialized from the file
QString str;
Q_INT32 a;
stream >> str >> a; // extract "the answer is" and 42