PDA

View Full Version : How to write the BYTE* data to a QFile?



Gokulnathvc
18th July 2011, 07:17
How to write the BYTE* data to a QFile? I have binary data in BYTE*, I want to write that data completely to QFile. How to perform this. Please help me

Santosh Reddy
18th July 2011, 07:33
I assume your BYTE is char type, then convert char* to QByteArray, something like this


char* data; //binary data
int length; //length of binary data
QFile file;

file.write(QByteArray(data, length));

Gokulnathvc
18th July 2011, 07:41
I am using BYTE* configid; How to write this to QFile.

FelixB
18th July 2011, 07:45
how do you fill the BYTE*?

Gokulnathvc
18th July 2011, 07:47
memcpy(configid,buff,len);
here buff is QByteArray.

Santosh Reddy
18th July 2011, 07:51
is there any problem using
QFile::write(QByteArray(configid, len));

Gokulnathvc
18th July 2011, 07:53
Yes, it just prints one ",". Thats all.

Santosh Reddy
18th July 2011, 07:57
Then check what is going wrong,

- check if len is proper, set a break point at write and check the value of len
- check QFile is opened in binary write mode

ChrisW67
18th July 2011, 09:24
memcpy(configid,buff,len);
here buff is QByteArray.

If you mean that buff is a pointer to a QByteArray then this will not do what you are intending. If buff is the value of QByteArray::constData() then you are copying the data from the QByteArray (assuming len is meaningful). In any case you have invented work for yourself. If you have the data already in a QByteArray then don't copy is somewhere else before trying to put it in a file.


// QByteArray buff;
QFile out("/tmp/dump.bin");
if (file.open(QIODevice::WriteOnly)) {
open.write(buff);
file.close();
}

Momergil
22nd November 2011, 12:54
Then check what is going wrong,

- check if len is proper, set a break point at write and check the value of len
- check QFile is opened in binary write mode

Hello!

How is it possible to "open in binary write mode"? The QIODevice::OpenMode allows only QFile::Text to be set, and no other option is available specifically to binary.

ChrisW67
23rd November 2011, 04:33
If you don't request the end-of-line translations with QIODevice::Text then no translation is done: it is a binary file... just a stream of bytes.