Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Overwrite specific bytes without reading the whole file, QFile
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    faenil
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    1
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      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

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • F Offline
        F Offline
        faenil
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sfilippidis
          wrote on last edited by
          #4

          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

          https://www.filippidis.name/

          1 Reply Last reply
          0
          • F Offline
            F Offline
            faenil
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sfilippidis
              wrote on last edited by
              #6

              [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! :-)

              https://www.filippidis.name/

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved