QDataStream::writeBytes

QDataStream &QDataStream::writeBytes(const char *s, uint len)
Writes the length specifier len and the buffer s to the stream and returns a reference to the stream.
The len is serialized as a quint32, followed by len bytes from s. Note that the data is not encoded.

注意,使用writeBytes函数写的文件,必须用readBytes函数读取,readBytes函数是个工厂函数,会调用new[]分配内存,因此需要调用delete[]进行删除内存,否则会泄露。

有特殊字符,比如∅ ㎡ 㘵,写入后再次读取,结果乱码。

QDataStream::writeRawData

int QDataStream::writeRawData(const char *s, int len)
Writes len bytes from s to the stream. Returns the number of bytes actually written, or -1 on error. The data is not encoded.

int QDataStream::writeRawData(const char * s, int len)int QDataStream::readRawData(char * s, int len)函数配对使用,用于写入最原始的内存,如果不想要Qt独有的一些信息,就需要用这两个函数。

QDataStream<<

QDataStream &QDataStream::operator<<(Type i)
Writes a signed byte, i, to the stream and returns a reference to the stream.

有特殊字符,比如∅ ㎡ 㘵,写入后再次读取,结果正确。

总结

在写出二进制时,如果需要仅仅写入原始的内存,记得使用int QDataStream::writeRawData(const char * s, int len)函数,而不是QDataStream::writeBytes函数!

有特殊字符,比如∅ ㎡ 㘵,使用QDataStream<<