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...