QFile/QDataStream 写入现有数据
我有一个文件,假设长度为 8 个字节。 例如,它看起来像这样:
22222222
现在,我首先读取 5 个字节并更改它们。对于前。到 11111
最后,我想将它们写入现有数据到文件中,所以我希望文件看起来像这样:
11111222
但我只得到 11111< /code>,因为文件被删除。如何禁用擦除? (也许这个问题存在,但找不到这样的问题)
I have got a file which is let's say 8 bytes length.
For example it looks like that:
22222222
Now, I read first let's say 5 bytes and changing them. For ex. to 11111
Finally, I want to write them ONTO EXCISTING DATA to the file, so I expect the file to look like that:
11111222
But I get only 11111
, because file is erased. How can I disable erasing? (Maybe this question exists, but couldn`t find question like this one)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您对文件的具体操作,您可能需要对其进行内存映射:
Depending on what you are exactly doing with the file, you might want to memory map it:
该函数写入数据。
this function write the data.
尝试使用
| 打开
,然后是QFile
QIODevice::AppendQFile::seek()
,然后在QFile
对象上创建QDataStream
。但请注意,QDataStream
将控制信息添加到输出中,因此这可能不会产生您想要的结果。另外,如果您想写入文本而不是二进制数据,请尝试使用 QTextStream。
Try to open the
QFile
with| QIODevice::Append
, thenQFile::seek()
, then create theQDataStream
on theQFile
object. But note thatQDataStream
adds control information to the output, so that probably doesn't result in exactly what you want.Also if you want to write text, not binary data, try with
QTextStream
.