Results 1 to 3 of 3

Thread: Reading and writing bytes in file

  1. #1
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Reading and writing bytes in file

    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?

    Qt Code:
    1. QList<QByteArray> _compressedArrayList;
    2.  
    3. QFile file1( "foo.xml" );
    4. file1.open( QFile::ReadOnly );
    5. QByteArray fileBytes = file1.readAll();
    6. QByteArray compressedFileBytes = qCompress( fileBytes );
    7. quint16 checksumBefore = qChecksum( compressedFileBytes.data(), compressedFileBytes.count() );
    8. _compressedArrayList << compressedFileBytes;
    9. file1.close();
    10.  
    11. QFile file( "out" );
    12. if( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
    13. {
    14. QDataStream fout( &file );
    15. foreach( QByteArray byteArray, _compressedArrayList )
    16. {
    17. fout << byteArray.data();
    18. }
    19. file.close();
    20. }
    21.  
    22. //..............
    23. //..............
    24. QFile packageFile( "out" );
    25. packageFile.open( QFile::ReadOnly );
    26. QByteArray compressedFileBytes = packageFile.read( compressedFileBytes.count() );
    27. quint16 checksumAfter = qChecksum( compressedFileBytes.data(), compressedFileBytes.count() );
    To copy to clipboard, switch view to plain text mode 
    Last edited by DiamonDogX; 20th May 2009 at 18:42.

  2. #2
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Reading and writing bytes in file

    I see two things:
    1. You are mixing QFile operations with QDataStream operations
    2. You are writing down a list of arrays, but you are reading only one array

  3. #3
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Reading and writing bytes in file

    Looks like you were right... the QDataStream mixing with QFile was throwing things off. Thx.

Similar Threads

  1. Function for reading and writing
    By merry in forum Newbie
    Replies: 2
    Last Post: 30th May 2007, 08:30
  2. serialising reading writing
    By TheKedge in forum General Programming
    Replies: 4
    Last Post: 5th April 2007, 17:17
  3. Replies: 6
    Last Post: 8th January 2007, 10:24
  4. XML file writing
    By mbjerkne in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2006, 19:04
  5. Replies: 6
    Last Post: 27th February 2006, 12:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.