Results 1 to 4 of 4

Thread: Reading/writing data to binary 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/writing data to binary file

    I am basically writing a bunch of qint32 values to a binary file and I want to be able to seek to an offset of the file and read a value back. So I am doing the following for writing and reading:

    Qt Code:
    1. QFile file( "MyFile" );
    2. if( file.open( QFile::WriteOnly | QFile::Truncate ) )
    3. {
    4. for( int i = 0; i < 10; i++ )
    5. {
    6. qint32 exampleNum = 42;
    7. file.write( ( char * )( &exampleNum ), sizeof( exampleNum) );
    8. }
    9. file.close();
    10. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. QFile file( "MyFile" );
    2. if( file.open( QFile::ReadOnly ) )
    3. {
    4. if( file.seek( 4) )
    5. {
    6. char * dataRead = new char[ sizeof( qint32 ) ];
    7. qint64 numBytesRead = file.read( dataRead, sizeof( qint32 ) );
    8. if( numBytesRead == sizeof( qint32 ) )
    9. {
    10. QByteArray dataBytes( dataRead );
    11. bool ok = false;
    12. qint32 value = dataBytes.toInt( &ok ); // "value" ends up 0
    13. if( ok )
    14. {
    15. cout << value; // never gets here
    16. }
    17. }
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    Seem to be having trouble reading a value back...

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Reading/writing data to binary file

    Hey, just a quick suggestion: you might want have a look at QDataStream. Could make your live much more easier.

  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/writing data to binary file

    Yeah I've used it before... doesn't seem to be helping me in this case...

  4. #4
    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/writing data to binary file

    I take that back! I mixed it in with the code, changed some things, and I can read and write the integers now...

Similar Threads

  1. Replies: 12
    Last Post: 31st October 2010, 17:08
  2. Read binary from file
    By weldpua2008 in forum Newbie
    Replies: 2
    Last Post: 3rd April 2009, 23:50
  3. Reading binary data
    By vermarajeev in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2007, 09:14
  4. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 18:10
  5. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 16:17

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.