Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Replacing some bytes in a file in binary mode

Replacing some bytes in a file in binary mode

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
8 Posts 5 Posters 0 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    sunny_vc
    wrote on last edited by
    #1

    Hi, I have a file which is in binary mode.(I am using stdio functions fwrie,fread,fseek) I have to read this file in binary mode and needs to replace some of the bytes in this file. It contains around 1000 entries with 2 bytes each. I know the location of bytes which is to be changed, like 100th entry. Is there anyway to change that specific bytes, without copying in to other file. As I have small memory I can not copy into other file and recopy with changed bytes. So on the fly i want to change specific bytes. please tell me if any method is there to do this.?

    Regards, Sunil Kumar

    C 1 Reply Last reply
    0
    • S sunny_vc

      Hi, I have a file which is in binary mode.(I am using stdio functions fwrie,fread,fseek) I have to read this file in binary mode and needs to replace some of the bytes in this file. It contains around 1000 entries with 2 bytes each. I know the location of bytes which is to be changed, like 100th entry. Is there anyway to change that specific bytes, without copying in to other file. As I have small memory I can not copy into other file and recopy with changed bytes. So on the fly i want to change specific bytes. please tell me if any method is there to do this.?

      Regards, Sunil Kumar

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      You may open the file for read/write (i.e. "r+") go to intended position with fseek and change the byte content with fwrite. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      S 1 Reply Last reply
      0
      • C CPallini

        You may open the file for read/write (i.e. "r+") go to intended position with fseek and change the byte content with fwrite. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        S Offline
        S Offline
        sunny_vc
        wrote on last edited by
        #3

        yeah.i've used the same thing.But that is not working.

        Regards, Sunil Kumar

        C I S 3 Replies Last reply
        0
        • S sunny_vc

          yeah.i've used the same thing.But that is not working.

          Regards, Sunil Kumar

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          My code (beware: error checking omitted for concision)

          FILE * fp = fopen("foo.bin", "rb+");
          fseek(fp, 10, SEEK_SET);
          fwrite("#", 1, 1, fp);
          fclose(fp);

          is working fine. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          modified on Friday, January 22, 2010 6:43 AM

          _ 1 Reply Last reply
          0
          • S sunny_vc

            yeah.i've used the same thing.But that is not working.

            Regards, Sunil Kumar

            I Offline
            I Offline
            iceman8616
            wrote on last edited by
            #5

            did you remember fflush() the buffers?

            1 Reply Last reply
            0
            • C CPallini

              My code (beware: error checking omitted for concision)

              FILE * fp = fopen("foo.bin", "rb+");
              fseek(fp, 10, SEEK_SET);
              fwrite("#", 1, 1, fp);
              fclose(fp);

              is working fine. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              modified on Friday, January 22, 2010 6:43 AM

              _ Offline
              _ Offline
              _Superman_
              wrote on last edited by
              #6

              Except that for binary you could do a "rb+".

              «_Superman_» I love work. It gives me something to do between weekends.
              Microsoft MVP (Visual C++)

              C 1 Reply Last reply
              0
              • S sunny_vc

                yeah.i've used the same thing.But that is not working.

                Regards, Sunil Kumar

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                As superman's said elsewhere in this thread, use binary mode when you open the file, using

                FILE * fp = fopen("foo.txt", "rb+");

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

                1 Reply Last reply
                0
                • _ _Superman_

                  Except that for binary you could do a "rb+".

                  «_Superman_» I love work. It gives me something to do between weekends.
                  Microsoft MVP (Visual C++)

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Oh, thanks... :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups