the difference between QDataStream and QFile
-
I have a problem with QTcpSocket.
- read the all bytes from the source file of the server( for example, 10000bytes)
- send these bytes to the client by QTcpSocket of QTcpServer
- QTcpSocket of the licent can receive these bytes correctly,10000bytes.
- create a file in the client and open with write mode.
- create a QDataStream and binds it with the file.
- write these bytes to the file with the QDataStream
And the problem is the final number of bytes written to the file will be a little more than 10000bytes. If I use QFile::Write() directly , there will be no problem.
I don't know why there is the difference of the bytes written between QDataStream::<< and QFile::Write()
-
@jgxy1123 If you just want to write a byte stream into a file there is no need to use QDataStream.
QDataStream is used to serialise/deserialise data structures, that's also why some additional data is written (for example the version of the format). -
Hi @jgxy1123,
And the problem is the final number of bytes written to the file will be a little more than 10000bytes.
Which is correct - it should be more than the 10000 bytes of data.
As @jsulm indicated, and per the docs,
QDataStream
is not raw data, but rather:A data stream is a binary stream of encoded information which is 100% independent of the host computer's operating system, CPU or byte order.
...
To take one example, achar *
string is written as a 32-bit integer equal to the length of the string including the\0
byte, followed by all the characters of the string including the\0
byte. When reading achar *
string, 4 bytes are read to create the 32-bit length value, then that many characters for thechar *
string including the\0
terminator are read.The exact format will depend on which
QDataStream::operator<<()
overload is being chosen by the compiler based on your actual code, but the the resulting stream will include things like length indicators, and, possibly have its endianness reversed, depending on the host CPU, etc.Cheers.