简体   繁体   English

无法读取写入的Qt QBuffer字节

[英]Qt QBuffer bytes written cannot be read

A bit of confusion here: I'm trying to do this: 这里有些混乱:我正在尝试这样做:

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";

This outputs the following: 输出以下内容:

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

That last line really confuses me. 最后一行确实让我感到困惑。 I thought the return value from the .write() method was supposed to say how many bytes were written? 我认为.write()方法的返回值应该说写了多少字节? Why would they not be available? 为什么它们不可用?

And, later, I attempt the following: 而且,稍后,我尝试以下操作:

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";

Considering the previous bytesAvailable result, the output of these calls aren't too surprising. 考虑到以前的bytesAvailable结果,这些调用的输出并不奇怪。 They output the following: 他们输出以下内容:

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

So I feel like I'm missing a step, and that you have to do something between writing and the data being available to read. 因此,我觉得我错过了一步,您必须在写入和读取数据之间做些事情。 Perhaps some sort of seek or something? 也许某种寻求之类的东西? But I seem to be missing where it says that in the documentation. 但是我似乎在文档中的位置不见了。

Any tips would be appreciated. 任何提示将不胜感激。 Thank you! 谢谢!

You need to seek back to the position you want to read from: 你需要寻求回到你想从中读取数据的位置:

_ourLogMessagesBuffer.seek(0);

Then you will be able to see an appropriate amount of bytesAvailable. 然后,您将能够看到适当数量的bytesAvailable。 If you think about as a (physical) pointer to a position on a tape, it makes sense. 如果您将其视为指向磁带上某个位置的(物理)指针,那么这是有道理的。 As you write, the pointer moves to the end where it can write more data. 在编写时,指针移至可以写入更多数据的末尾。 Any tape ahead of the pointer is "blank"; 指针前面的任何胶带都是“空白”; there's nothing to read (for a "blank" tape, a new or empty buffer). 没有什么要读的(对于“空白”磁带,是新缓冲区还是空缓冲区)。

When just writing, the position is automatically updated for you. 仅在书写时,职位会自动为您更新。 But if you want to read data you already wrote, you need to tell it to go back. 但是,如果您想读取已经写入的数据,则需要告诉它返回。

An exception to this is with, say, a file format. 例外情况是文件格式。 If we are modifying an existing file, we could update a fixed-length timestamp in one part, then immediately read a couple bytes denoting the length of an "author" string, and then read that string in. For that, we would not need a seek as all the data is contiguous, and the write and read functions handle moving the position within the file (buffer) automatically. 如果要修改现有文件,则可以在一个部分中更新固定长度的时间戳,然后立即读取几个字节,这些字节表示“作者”字符串的长度,然后读入该字符串。为此,我们不需要seek因为所有数据都是连续的,并且writeread功能可自动处理文件(缓冲区)中的位置。

If you have non-contiguous reads/writes, you need to seek . 如果您具有不连续的读/写,则需要seek Otherwise, it can't read your mind on where you want to read from. 否则,它将无法读懂您想从哪里阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM