Link to home
Start Free TrialLog in
Avatar of PeterVarain
PeterVarain

asked on

Replace value in QByteArray & QDataStream

Hi experts,

I'm trying without success to replace some values in a byte array or in a data stream in QT 4.7

Here's my situation:
I have a binary file with data. Each two bytes of this file represent a signed integer value. I need to replace all the integer values which are 0 with 22 and save the file with the new values.

Any ideas how to proceed ?

Thanks,
Peter
Avatar of HooKooDooKu
HooKooDooKu

Unless there is an issue with byte order, you should be able to open the file in read/write mode, and call the Read function passing the pointer to a DWORD variable and reading two bytes.  If the variable has a value of 0, seek backwards 2 bytes and write the value 22.

CFile f;
f.Open( strFileName, CFile::modeRead | CFile::modeWrite );
DWORD i;
const DWORD v22 = 22;
while( f.Read( &i, 2 ) == 2 )
{
  if( i == 0 )
  {
    f.Seek( -2, CFile::current );
    f.Write( &v22 );
  }
}

If there is an issue with byte order, then you just need to replace v22=22 with v22=0x1600;
Opps... that should have been something like
f.Write( &v22, 2 )

and you might have to type case the pointers to get something like this to properly compile.
Avatar of PeterVarain

ASKER

Sorry, I haven't explained very clear what I want to do here...

The file which is saved is not the same file (the source file). I need to move all the data from the source file to a QByteArray and in the process change the 0 values to 22. Then, I need to compress this byte array with qCompress and only then write it to a newly created file.

So, my problem is that I have to move the data from the source file to the byte array and change to zeros...

All the best,
Peter
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
there is a ) missing at right of while statement.

Sara
Thanks Sara!