Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] Overwrite specific bytes without reading the whole file, QFile



  • Hi guys :)

    I have been looking for a way to overwrite specific files in a file, but could only find something about memory mapping the file...let me explain :)

    let's suppose I want to overwrite byte 0x51B...I use this code

    @ QFile cre("C:/stubs/200159ED.cre");
    if (!cre.open(QIODevice::WriteOnly)) return -10;
    cre.seek(0x51B);
    QDataStream crestream(&cre;);
    qint8 attempt = 1;
    crestream << attempt;
    cre.close()@

    the problem is the file is first erased, and then it's filled with 0s, until 0x51B is reached, which is correctly set to 01...

    Is there any way I can avoid erasing the whole file before writing into it?

    I'd also like to avoid reading the whole file, changing some bits, and then writing it back to a new file, because the file could be pretty big :)

    Thanks in advance :)

    faenil



  • Yep. WriteOnly will silently truncate. Open the file with ReadWrite.

    cf. https://qt.gitorious.org/qt/qt/blobs/master/src/corelib/io/qfsfileengine.cpp#line201



  • thank you very much :) and thanks for the precious link :)



  • If your initial question is answered and you consider the issue solved, please edit your initial post to change the title from

    Overwrite specific bytes without reading the whole file, QFile

    to

    [SOLVED] Overwrite specific bytes without reading the whole file, QFile



  • I'll try it tomorrow morning and check if it's solved ;) thanks for the tip anyway ;)



  • [quote author="faenil" date="1312059979"]I'll try it tomorrow morning and check if it's solved ;) thanks for the tip anyway ;) [/quote]

    Even better! :-)


Log in to reply