Solved

QT, Copy data from QDataStream to QByteArray

Posted on 2011-03-11
2
1,852 Views
Last Modified: 2012-05-11
Hi experts,

I have a QDataStream with data read from a file. Every 2 bytes in this file are a signed integer value.

How do I copy the data in the data stream to a QByteArray ?

For example:

QByteArray inputf1;
QDataStream in1(&filein1);

I need to copy the data from input in1 to inputf1.

Thanks,
Peter
0
Comment
Question by:PeterVarain
2 Comments
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 35107272
you could use resize function of QByteArray to get proper size and then cast off constness off from constData function to get a writeable char buffer.

  QByteArray inputf1;
  QDataStream in1(&filein1);
   inputf1.resize(4096);
   in1.readRawData(const_cast<char*)(inputf1.constData()), 4096);

but that is a bit unsafe because of the cast.

if memory isn't an issue i would use a temporary char buffer for read and move it to QByteArray using the insert function after read.

Sara



0
 

Author Closing Comment

by:PeterVarain
ID: 35114839
Thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

827 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