PDA

View Full Version : Reading and writing bytes in file



DiamonDogX
20th May 2009, 19:10
I am attempting to read the bytes from one file, compress them, stick them in another file, and read that file back in at a later time to get those bytes. But the bytes I write don't seem to be the same bytes I read back in and they should be. I tried computing a checksum to verify but the problem I see is that checksumBefore does not equal checksumAfter (see below). What am I missing?


QList<QByteArray> _compressedArrayList;

QFile file1( "foo.xml" );
file1.open( QFile::ReadOnly );
QByteArray fileBytes = file1.readAll();
QByteArray compressedFileBytes = qCompress( fileBytes );
quint16 checksumBefore = qChecksum( compressedFileBytes.data(), compressedFileBytes.count() );
_compressedArrayList << compressedFileBytes;
file1.close();

QFile file( "out" );
if( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QDataStream fout( &file );
foreach( QByteArray byteArray, _compressedArrayList )
{
fout << byteArray.data();
}
file.close();
}

//..............
//..............
QFile packageFile( "out" );
packageFile.open( QFile::ReadOnly );
QByteArray compressedFileBytes = packageFile.read( compressedFileBytes.count() );
quint16 checksumAfter = qChecksum( compressedFileBytes.data(), compressedFileBytes.count() );

auba
20th May 2009, 20:14
I see two things:

You are mixing QFile operations with QDataStream operations
You are writing down a list of arrays, but you are reading only one array

DiamonDogX
20th May 2009, 21:25
Looks like you were right... the QDataStream mixing with QFile was throwing things off. Thx. :)