【发布时间】:2015-04-14 20:29:32
【问题描述】:
根据the documentation for readBytes()(在Qt 5.4 的QDataStream 中),我希望以下代码将input_array 复制到新分配的内存中,并将raw 指向副本:
QByteArray input_array{"\x01\x02\x03\x04qwertyuiop"};
QDataStream unmarshaller{&input_array, QIODevice::ReadOnly};
char* raw;
uint length;
unmarshaller.readBytes(raw, length);
qDebug() << "raw null? " << (raw == nullptr) << " ; length = " << length << endl;
...但是代码打印出raw null? true ; length = 0,表示没有从输入数组中读取任何字节。
这是为什么?我对readBytes() 有什么误解?
【问题讨论】:
-
如果你不指定大小,
QByteArray构造函数需要一个以 nul 结尾的字符数组。 -
@cmannett85 啊,谢谢。但是,如果我首先使用
>>读取一系列quint8s,我会得到预期的1、2、3、4,这似乎暗示我确实在构建预期的 QByteArray ....对吗? -
@cmannett85 另外,我可以使用
qDebug()打印QByteArray。 -
@cmannett85 最后,在
input_array构造函数字符串的末尾添加\0似乎不会改变代码的行为。