查询了一番,QFile好像没有办法直接更改某个位置的值,无奈之下只能先读源文件,然后保存为新的文件。如下:
QString filename = fileNew->fileName();//由于是正在写的文件,写到最后需要更改前面某个位置的值
fileNew->close();
fileNew->open(QIODevice::ReadOnly);
//需要更改第9和第10个字节
QByteArray head1 = fileNew->read(8);//先读前面8个字节
fileNew->seek(10);//跳到第10个字节
QByteArray head2 = fileNew->read(245);//再接着读245个字节
fileNew->seek(255);//跳到255 此处不跳应该也是可以的 上面一句代码应该已经跳到该处了
QByteArray arraylast = fileNew->readAll(); //读取剩下的数据
fileNew->close();
delete fileNew;
fileNew = NULL;
//重写文件更改总帧数
QFile* filenewdst = new QFile(filename);
QDataStream out(filenewdst);
if (!filenewdst->open(QIODevice::Truncate | QIODevice::WriteOnly))
{
return 0;
}
QByteArray data;
data.resize(2);
int iframe = 0x1010;//随便设个值
data[0] = (uchar)((iframe & 0xff00) >> 8);
data[1] = (uchar)(iframe & 0xff);
out.writeRawData(head1, head1.size());//写入读取的数据
out.writeRawData(data, 2);
out.writeRawData(head2, head2.size());//写入更改的两个字节数据
out.writeRawData(arraylast, arraylast.size());//写入读取的数据
filenewdst->close();