PDA

View Full Version : i write a test program whit QDatastream read and write the binary file but it's dif



tsuibin
2nd November 2010, 03:26
i write a test program whit QDatastream
read and write the binary file
but
it's different

my code



QFile file("1.png");
file.open(QIODevice::ReadOnly);
QFile tmp("2");
tmp.open(QIODevice::WriteOnly);
QDataStream out(&tmp);
out << file.readAll();



diff 1.png 2
output:
Binary files 1.png and 2 differ
why ?

ChrisW67
2nd November 2010, 04:33
They differ because a QDataStream is not a raw byte stream. You call QIODevice::readAll(), which returns a QByteArray, and then ask QDataStream to serialise the QByteArray to a stream. QDataStream will output sufficient information to reconstruct the QByteArray, including any internal structures, by reading from QDataStream later on any platform. You really just want the base QIODevice methods to read and write raw bytes.

If you just want to copy a file then QFile::copy() will be of interest.

tsuibin
2nd November 2010, 05:15
what you have said is very helpful, thank you very much, ChrisW67.