Solved

Replace value in QByteArray & QDataStream

Posted on 2011-03-10
6
1,326 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:PeterVarain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 35098671
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;
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 35098683
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.
0
 

Author Comment

by:PeterVarain
ID: 35098977
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Accepted Solution

by:
sarabande earned 500 total points
ID: 35107498
you could read the file using std::ifstream in binary mode:

   std::ifstream infile("xxx.dat", std::ios_base::in | std::ios_base::binary);
   qint16 q16;
   while (infile.read((char*)(&q16), sizeof(q16))
   {
   }

note the qint16 should be 2 bytes what i have taken for granted.

the in loop you could check on 0 and replace by 22. then add to QByteArray

  QByteArray qarr;
  ...
       
        if (q16 == 0)
            q16 = 22;
       qarr.append((char*)(&q16), 2);  

note, the QByteArray would get binary data that way, same as in the file. if you need hexstring conversion you mast do a conversion first.

Sara

0
 
LVL 34

Expert Comment

by:sarabande
ID: 35107504
there is a ) missing at right of while statement.

Sara
0
 

Author Closing Comment

by:PeterVarain
ID: 35114953
Thanks Sara!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question