写Qt QBuffer字节无法读取

时间:2018-02-26 15:17:58

标签: c++ qt qiodevice qbuffer

这里有点混乱:我正在尝试这样做:

QBuffer _ourLogMessageBuffer;
QByteArray theLogMessage;
...
qDebug() << "Writing " << theLogMessage.size() << " bytes to buffer\n";
qint64 numberOfBytes - _ourLogMessagesBuffer.write(theLogMessage);
qDebug() << "Wrote " << numberOfBytes << " bytes to buffer\n";
qDebug() << "Buffer has " << _ourLogMessagesBuffer.bytesAvailable()
         << " bytes available to read (after write)\n";

这输出以下内容:

Writing 196 bytes to buffer
Wrote 196 bytes to buffer
Buffer has 0 bytes available to read (after write)

最后一行真让我困惑。我认为.write()方法的返回值应该说明写了多少字节?为什么他们不可用?

然后,我尝试以下方法:

qDebug() << "Buffer has " << _ourLogMessagesBuffer.bytesAvailable()
         << " bytes available to read (before read)\n";
char logMessageBytes[565];
qint64 numberOfBytes = _ourLogMessagesBuffer.read(logMessageBytes, 565);
qDebug() << "Read " << numberOfBytes << " bytes from buffer\n";

考虑到之前的bytesAvailable结果,这些调用的输出并不太令人惊讶。他们输出以下内容:

Buffer has 0 bytes available to read (before read)
Read 0 bytes from buffer

所以我觉得我错过了一步,你必须在写作和可读数据之间做些什么。也许是某种寻求或某种东西?但我似乎在文档中所说的内容中遗漏了。

任何提示将不胜感激。谢谢!

0 个答案:

没有答案