【发布时间】:2016-04-05 19:10:43
【问题描述】:
我正在尝试使用 QDataStream 和 QByteArray 读取二进制数据文件,进行一些更改,然后另存为新文件。
我有以下几点:
QDataStream in_datastream(&file);
QByteArray fileByteArray = file.readAll();
//find my insertion point
int pos = fileByteArray.indexOf(magic_num, 0);
//move to insertion point, minus 4 bytes (size of an integer)
file.seek(pos-4);
int current_val;
//check what the value is here
in_datastream >> current_val;
//now, I want to replace that value..but how?
此时我已经尝试了几件事,但似乎无法弄清楚如何使其发挥作用。我想在 QByteArray 的 (pos-4) 处插入整数 5000。
//remove 4 bytes..this seems to work
fileByteArray.remove(pos-4, 4);
//actually inserts garbage.
fileByteArray.insert(pos-4, newInteger);
如果我尝试插入 4 个字符的字符串“TEST”,它们都会正确插入。我想我在尝试将整数放入其中时遇到了一些类型转换问题。
【问题讨论】:
标签: c++ qt qbytearray