Re: replacing text data in a binary file

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Jay B. Harlow [MVP - Outlook] (Jay_Harlow_MVP_at_msn.com)
Date: 06/09/04


Date: Wed, 9 Jun 2004 09:24:40 -0500

Adam,
&hFF (-1) is a valid Byte value, returning an integer allows -1 for EOF &
&HFF for the byte.

Hope this helps
Jay

"Adam J. Schaff" <aschaff@cascocdev.com> wrote in message
news:OU0vfXiTEHA.3944@tk2msftngp13.phx.gbl...
> p.s. I just notice that value is an integer. That explains the cbyte in
your
> code, but now I'm confused why ReadByte returns an integer?? Is it
something
> to do with testing for EOF? (-1 isn't a typical byte value it occurs to me
> ;)
>
> "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message
> news:uAuSP4hTEHA.1984@TK2MSFTNGP12.phx.gbl...
> > Adam,
> > If you are editing a binary file I would recommend opening the file with
a
> > BinaryReader or even just FileStream directly.
> >
> > > Dim sr As New StreamReader(fs, System.Text.Encoding.UTF8)
> >
> > If you use a StreamReader, you are actually converting the file into
Text
> > (8bit bytes to 16 bit chars) then converting the file back into Binary.
> >
> > My two concerns with using a StreamReader are:
> > 1. depending on what the binary file actually contains (an exe for
> example)
> > removing or adding characters may actually corrupt the file, as the file
> > contains length & offset fields...
> > 2. Loosing certain bytes, ones that are not translated nicely from
> arbitrary
> > bytes back & forth to Unicode. For example using Encoding.ASCII will
trash
> > your file as ASCII is 7 bit, you will loose all the high order bits. I
> > suspect with UTF8 you will be OK, however it just does not feel right...
> >
> > Here is a simple program that will copy a file one byte at a time, the
> trick
> > is going to be modify it so it looks for "g:\" one byte at a time and
> skips
> > those bytes, only if all three are found... Especially if you want to
> ensure
> > they are prefaced with "file//".
> >
> > Dim input As New FileStream("Only Fools.fpl", FileMode.Open)
> > Dim output As New FileStream("Only Fools2.fpl", FileMode.Create)
> > Dim value As Integer
> > value = input.ReadByte()
> > Do Until value = -1
> > output.WriteByte(CByte(value))
> > value = input.ReadByte()
> > Loop
> > input.Close()
> > output.Close()
> >
> > Hope this helps
> > Jay
> >
> > "Adam J. Schaff" <abjunk@adelphia.net> wrote in message
> > news:OkxC3hdTEHA.1508@TK2MSFTNGP11.phx.gbl...
> > > I am writing a quick program to edit a binary file that contains file
> > paths
> > > (amongst other things). If I look at the files in notepad, they look
> like:
> > >
> > >
> <gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish>
> > > etc.
> > >
> > > I want to remove the "g:\" from the file paths. I wrote a console app
> that
> > > successfully reads the file and writes a duplicate of it, but fails
for
> > some
> > > reason to do the "replacing" of the "g:\". The code follows with a
note
> > > showing the line that is not working. When I look at the "s" variable
in
> > > break mode, I see that VB does not show the entire file contents, even
> > > though when I write "s" to the second file stream, the entire original
> > file
> > > is duplicated. I suppose this is because the file content isn't
intended
> > to
> > > be interpreted as a string (its binary after all). It is probably
> hitting
> > > some unfriendly bytes that it can't interpret for string operations
like
> > > Replace. If that's the case, maybe I need to interact with it a
> character
> > at
> > > a time, although I'm not sure how I would do a replace that way. Any
> ideas
> > > or code would be greatly appreciated. As you can tell, I don't do much
> > > binary file I/O.
> > >
> > > Sub Main()
> > > 'read the binary file into a string var
> > > Dim fs As New FileStream("C:\Source\Sample and
> > > Demo\Foobar\EditFpl\Only Fools.fpl", FileMode.Open, FileAccess.Read)
> > > Dim sr As New StreamReader(fs, System.Text.Encoding.UTF8)
> > > Dim s As String = sr.ReadToEnd
> > > sr.Close()
> > > fs.Close()
> > >
> > > 'remove the hard-coded drive letter specification
> > > s.Replace("file://G:\", "file://") 'THIS LINE DOES NOT WORK
> > >
> > > 'write a new binary file with my changes
> > > Dim fs2 As New FileStream("C:\Source\Sample and
> > > Demo\Foobar\EditFpl\Only Fools2.fpl", FileMode.CreateNew,
> > FileAccess.Write)
> > > Dim sw As New StreamWriter(fs2, System.Text.Encoding.UTF8)
> > > sw.Write(s)
> > >
> > > sw.Close()
> > > fs2.Close()
> > > End Sub
> > >
> > >
> >
> >
>
>



Relevant Pages

  • Re: fseek
    ... >>a text stream to a binary file or vice versa. ... >>binary file in text mode or vice versa results in undefined behavior. ... newline characters are treated the same as ... On IBM's mainframe OSs lines never have newline characters on them, ...
    (comp.lang.c)
  • Re: Replacing characters in file
    ... Well, if it is not a text file but a binary file, then it isn't very ... meaningful to talk about characters. ... Computers are notorious at being extremely pedantic. ... It doesn't work in SED because SED aborts when it hits the first EOF ...
    (comp.lang.perl.misc)
  • Re: Delete key leaves ghost characters at command prompt in xterm
    ... incorrect characters, and output characters to be garbled. ... remember how xterm behaved in those circumstances. ... But I am wondering if I might have cat a binary file by ... page (the pager used by man usually is configured to reset the display ...
    (comp.os.linux.misc)
  • Re: Replacing characters in file
    ... Well, if it is not a text file but a binary file, then it isn't very ... meaningful to talk about characters. ... It doesn't work in SED because SED aborts when it hits the first EOF ... downloading Active Perl and see what happens there, ...
    (comp.lang.perl.misc)