View Full Version : Append/Prepend QString to QByteArray
ermustaqbal
1st August 2011, 06:51
Hi,
I have a question about QByteArray.
assume that i have a source like this
QByteArray ba;
QString inName = QFileDialog::getOpenFileName(this, "Load a file");
QFile *fileIn = new QFile(inName);
fileIn->open(QIODevice::ReadOnly);
ba = fileIn->readAll();
"ba" is a QByteArray, i want to append QString after ba. How can i do that? because when i use QByteArray::append() i can't see anything change, and after i try to write ba as a file again, it is still the same like before.
Thanks in advanced,
ps: my file is binary(video) file
mcosta
1st August 2011, 08:58
can you post your code???
ermustaqbal
1st August 2011, 09:08
can you post your code???
QByteArray ba;
QString inName = QFileDialog::getOpenFileName(this, "Load a file");
QFile *fileIn = new QFile(inName);
fileIn->open(QIODevice::ReadOnly);
ba = fileIn->readAll();
qDebug() << ba;
qDebug() << ba.length();
fileIn->close();
QByteArray acba = ba;
QString key = "thisisakey";
acba = acba.append(key);
qDebug() << acba; //i hope here will go "thisisakey" appended
QString outName = QFileDialog::getSaveFileName(this, "Save a file");
QFile *fileOut = new QFile(outName);
fileOut->open(QIODevice::WriteOnly);
fileOut->write(acba); // i will fail save the same file as before because i have append it with key,
// but the result it is success, and it means there is no change.
fileOut->flush();
fileOut->close();
wysota
1st August 2011, 11:58
What does acba.size() return after line 15 compared to its result before this line?
ermustaqbal
2nd August 2011, 04:01
What does acba.size() return after line 15 compared to its result before this line?
wysota after i print it out i have something like this :
before append: 26246026
after append: 26246036
it means the key succesfully appended?
Added after 54 minutes:
Hohoho got it, thanks wysota you make me think about size ... thanks a lot ...
mcosta
2nd August 2011, 07:19
size increeased of 10 (size of "thisisakey");
This code works for me.
QByteArray ba ("1234567890");
qDebug () << ba;
ba = ba.append(QString("Hello World"));
qDebug () << ba;
If your file is a binary one, writing on console is wrong because at the first 0x00 byte the output will be truncated.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.