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:
if( file.
open( QFile::WriteOnly |
QFile::Truncate ) ) {
for( int i = 0; i < 10; i++ )
{
qint32 exampleNum = 42;
file.write( ( char * )( &exampleNum ), sizeof( exampleNum) );
}
file.close();
}
QFile file( "MyFile" );
if( file.open( QFile::WriteOnly | QFile::Truncate ) )
{
for( int i = 0; i < 10; i++ )
{
qint32 exampleNum = 42;
file.write( ( char * )( &exampleNum ), sizeof( exampleNum) );
}
file.close();
}
To copy to clipboard, switch view to plain text mode
if( file.
open( QFile::ReadOnly ) ) {
if( file.seek( 4) )
{
char * dataRead = new char[ sizeof( qint32 ) ];
qint64 numBytesRead = file.read( dataRead, sizeof( qint32 ) );
if( numBytesRead == sizeof( qint32 ) )
{
bool ok = false;
qint32 value = dataBytes.toInt( &ok ); // "value" ends up 0
if( ok )
{
cout << value; // never gets here
}
}
}
}
QFile file( "MyFile" );
if( file.open( QFile::ReadOnly ) )
{
if( file.seek( 4) )
{
char * dataRead = new char[ sizeof( qint32 ) ];
qint64 numBytesRead = file.read( dataRead, sizeof( qint32 ) );
if( numBytesRead == sizeof( qint32 ) )
{
QByteArray dataBytes( dataRead );
bool ok = false;
qint32 value = dataBytes.toInt( &ok ); // "value" ends up 0
if( ok )
{
cout << value; // never gets here
}
}
}
}
To copy to clipboard, switch view to plain text mode
Seem to be having trouble reading a value back...
Bookmarks