QFile/QDataStream writing on existing data
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 di开发者_如何学Pythonsable erasing? (Maybe this question exists, but couldn`t find question like this one)
Depending on what you are exactly doing with the file, you might want to memory map it:
QFile f("The file");
f.open(QIODevice::ReadWrite);
uchar *buffer = f.map(0, 5);
// The following line will edit (both read from and write to)
// the file without clearing it first:
for (int i=0; i<5; ++i) buffer[i] -= 1;
f.unmap(buffer);
f.close();
void fileopen()
{
QDataStream Input(&file);
Input>>"11111";
Input>>"22222";
file.close();
}
this function write the data.
QDataStream &operator<<(QDataStream &ds,const QString &data)
{
ds<<data.toLatin1().data();
ds<<data.toLatin1().data();
return ds;
}
Try to open the QFile
with | QIODevice::Append
, then QFile::seek()
, then create the QDataStream
on the QFile
object. But note that QDataStream
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
.
精彩评论