Solved

Replace value in QByteArray & QDataStream

Posted on 2011-03-10
6
1,249 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 33

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 33

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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é.
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

777 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