overwrite binary data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christine Nguyen

    overwrite binary data

    I've written a program in VB.net. I need to open a file in binary mode and
    edit it. I cannot find a way to partially overwrite existing data. There
    are certain hex values I need to replace in the file. I am looking at the
    System.IO.Binar yWriter class. There only seems to be a way to append or
    completely truncate the contents of a file. Does anybody have an idea how I
    might accomplish this??

    Thanks,
    Christine


  • Christine Nguyen

    #2
    Re: overwrite binary data

    I should state that I actually want to delete parts of the binary data also
    (but not all of it).

    -Christine



    "Christine Nguyen" <hch_nguyen@hot mail.com> wrote in message
    news:e7denP0XDH A.1736@TK2MSFTN GP10.phx.gbl...[color=blue]
    > I've written a program in VB.net. I need to open a file in binary mode[/color]
    and[color=blue]
    > edit it. I cannot find a way to partially overwrite existing data. There
    > are certain hex values I need to replace in the file. I am looking at the
    > System.IO.Binar yWriter class. There only seems to be a way to append or
    > completely truncate the contents of a file. Does anybody have an idea how[/color]
    I[color=blue]
    > might accomplish this??
    >
    > Thanks,
    > Christine
    >
    >[/color]


    Comment

    • Chris Devol

      #3
      Re: overwrite binary data


      "Christine Nguyen" <hch_nguyen@hot mail.com> wrote in message
      news:%23eaopZ0X DHA.1744@TK2MSF TNGP12.phx.gbl. ..[color=blue]
      > I should state that I actually want to delete parts of the binary data also
      > (but not all of it).
      >
      > -Christine
      >
      > "Christine Nguyen" <hch_nguyen@hot mail.com> wrote in message
      > news:e7denP0XDH A.1736@TK2MSFTN GP10.phx.gbl...[color=green]
      > > I've written a program in VB.net. I need to open a file in binary mode[/color]
      > and[color=green]
      > > edit it. I cannot find a way to partially overwrite existing data. There
      > > are certain hex values I need to replace in the file. I am looking at the
      > > System.IO.Binar yWriter class. There only seems to be a way to append or
      > > completely truncate the contents of a file. Does anybody have an idea how[/color]
      > I[color=green]
      > > might accomplish this??
      > >
      > > Thanks,
      > > Christine[/color][/color]

      I don't think you can edit a file directly on the disk.

      You have to load the file into an array or other structure using a streamreader,
      then operate on it, and then overwrite the file using a streamwriter.


      Comment

      • Chris Devol

        #4
        Re: overwrite binary data

        "Jon Skeet" <skeet@pobox.co m> wrote in message
        news:MPG.19a06f 76251136498a2c7 @news.microsoft .com...[color=blue]
        > Chris Devol <xyz@defghijk.l mn> wrote:[color=green]
        > > I don't think you can edit a file directly on the disk.[/color]
        >
        > Yes you can - there's no problem doing that. You can't insert or delete
        > bits from the start of middle of the file, of course, but that's a
        > different matter.[/color]

        It seems to me that inserting and deleting is what "editing" is. So if you can't
        insert/delete then you can't edit. Or am I missing something?
        [color=blue][color=green]
        > > You have to load the file into an array or other structure using a
        > > streamreader, then operate on it, and then overwrite the file using
        > > a streamwriter.[/color]
        >
        > Note that StreamReader and StreamWriter are meant for text operations,
        > not the binary operations Christine is interested in.[/color]

        That's true. I should have said "BinaryReader". .........


        Comment

        • Jon Skeet

          #5
          Re: overwrite binary data

          Chris Devol <xyz@defghijk.l mn> wrote:[color=blue][color=green]
          > > Chris Devol <xyz@defghijk.l mn> wrote:[color=darkred]
          > > > I don't think you can edit a file directly on the disk.[/color]
          > >
          > > Yes you can - there's no problem doing that. You can't insert or delete
          > > bits from the start of middle of the file, of course, but that's a
          > > different matter.[/color]
          >
          > It seems to me that inserting and deleting is what "editing" is. So
          > if you can't insert/delete then you can't edit. Or am I missing something?[/color]

          Yes. You can overwrite. Consider a file which has fixed length records
          - it's perfectly possible to overwrite those records with new ones.
          Alternatively, you might have something which goes through an assembly
          marking all public methods as internal, or vice versa - something which
          just requires changing a single byte for each method. No need to read
          the whole file in and write it all out again.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Christine Nguyen

            #6
            Re: overwrite binary data

            Hi,

            I accomplished using the FileStream Class. It will do binary operations and
            insert without copying to a temp file and will delete at the tail end of the
            file using Filestream.SetL ength.

            Thanks,
            Christine


            "Chris Dunaway" <dunawayc@_lunc hmeat_sbcglobal .net> wrote in message
            news:Xns93D579C 8AF076CAD@207.4 6.248.16...[color=blue]
            > "Christine Nguyen" <hch_nguyen@hot mail.com> wrote in
            > news:e7denP0XDH A.1736@TK2MSFTN GP10.phx.gbl:
            >[color=green]
            > > I've written a program in VB.net. I need to open a file in binary
            > > mode and edit it. I cannot find a way to partially overwrite existing
            > > data. There are certain hex values I need to replace in the file. I
            > > am looking at the System.IO.Binar yWriter class. There only seems to
            > > be a way to append or completely truncate the contents of a file. Does
            > > anybody have an idea how I might accomplish this??
            > >
            > >[/color]
            >
            > The BinaryWriter class has a seek method which you can use to position the
            > file pointer at any point and overwrite the data at that position.
            >
            > As far as inserting, you will have to use a temp file and read all the[/color]
            data[color=blue]
            > up to the insertion point, write that data to the temp file, then write[/color]
            the[color=blue]
            > data you want to insert to the temp file, and finally read the remaining
            > data and write it to the temp file. Assuming all has gone well, delete[/color]
            the[color=blue]
            > original file and rename the temp file.
            >
            > A similar procedure must be used to delete information from a file.
            >
            > Chris
            >
            >
            > --
            > If you don't like lunchmeat, please remove it from my e-mail address to
            > send me an e-mail[/color]


            Comment

            Working...