Adam,
For a binary file, it really depends on the encoding, which I would try to
avoid! If you were using UTF8 and it worked then there is 1 char per byte.
If you need UTF16, then you have (16bit) Unicode itself & there are 2 bytes
per char (in the file), there is also a UTF32 ;-). In other words do not
confuse how a string is represented in memory and how text is encoded in a
file. Binary files could actually have multiple encodings of text. Consider
a file that stores the code page & the length of a string, followed by the
code page encoded value of the string... Speaking of which if you binary
file stores the length of the string, removing the "g:\" may cause problems
;-)
If you start using ReadChar & WriteChar you are back to translating to Text.
Assuming your file is not EBCDIC & you are not using any extended character
(an umlated a for example "ä"). A single byte in your file will contain a
single character. EBCDIC you still have 1 byte per single character however
its bit representation are different, extended characters (ANSI code pages)
are single character per byte, but bytes 128 to 255 have different bit
representations...
You can then use Asc or AscW to convert a char into a integer/byte.
[color=blue][color=green]
> > value = input.ReadByte()
> > Do Until value = -1[/color][/color]
if value = AscW("G"c)
' found a G, look for a :
End if[color=blue][color=green]
> > output.WriteByte(CByte(value))
> > value = input.ReadByte()
> > Loop[/color][/color]
For information on Unicode and other character sets see:
http://www.yoda.arachsys.com/csharp/unicode.html
A couple other articles that may help:
http://www.yoda.arachsys.com/csharp/...ngunicode.html
Hope this helps
Jay
"Adam J. Schaff" <aschaff@cascocdev.com> wrote in message
news:evjzbWiTEHA.1472@TK2MSFTNGP12.phx.gbl...[color=blue]
> Jay,
>
> Thanks for the code. I looked at using the binary reader, but couldn't
> figure out how to tell when it reaches EOF. Had no idea about the -1[/color]
trick.[color=blue]
> That said, I'm still not sure what the code would look like to test one[/color]
byte[color=blue]
> at a time for a sequence of 6 characters, particularly if those characters
> are stored as 2 bytes each. Hmm, I suppose there is a ReadChar method, but
> if I use that instead, I couldn't use the -1 trick. Also, how would I[/color]
write[color=blue]
> it back to the output file if I use ReadChar instead of ReadByte? Is there[/color]
a[color=blue]
> WriteChar, and will it cause problems if some of the data I'm reading with
> ReadChar isn't really a char? Ack! I'm just too new to all this. I don't
> even understand the use of cbyte in your code (isn't it a byte already?).
> Well, at least I have food for thought. Thanks again for the advice. I can
> see I have a lot to learn.
>
> Even if I do stay with the stream reader, I'll take your worries to heart
> and will add code to backup the file before I play with it. That's just[/color]
good[color=blue]
> common sense.
>
>
> "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message
> news:uAuSP4hTEHA.1984@TK2MSFTNGP12.phx.gbl...[color=green]
> > Adam,
> > If you are editing a binary file I would recommend opening the file with[/color][/color]
a[color=blue][color=green]
> > BinaryReader or even just FileStream directly.
> >[color=darkred]
> > > Dim sr As New StreamReader(fs, System.Text.Encoding.UTF8)[/color]
> >
> > If you use a StreamReader, you are actually converting the file into[/color][/color]
Text[color=blue][color=green]
> > (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[/color]
> example)[color=green]
> > 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[/color]
> arbitrary[color=green]
> > bytes back & forth to Unicode. For example using Encoding.ASCII will[/color][/color]
trash[color=blue][color=green]
> > 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[/color]
> trick[color=green]
> > is going to be modify it so it looks for "g:\" one byte at a time and[/color]
> skips[color=green]
> > those bytes, only if all three are found... Especially if you want to[/color]
> ensure[color=green]
> > 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...[color=darkred]
> > > I am writing a quick program to edit a binary file that contains file[/color]
> > paths[color=darkred]
> > > (amongst other things). If I look at the files in notepad, they look[/color][/color]
> like:[color=green][color=darkred]
> > >
> > >[/color][/color]
> <gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish>[color=green][color=darkred]
> > > etc.
> > >
> > > I want to remove the "g:\" from the file paths. I wrote a console app[/color][/color]
> that[color=green][color=darkred]
> > > successfully reads the file and writes a duplicate of it, but fails[/color][/color][/color]
for[color=blue][color=green]
> > some[color=darkred]
> > > reason to do the "replacing" of the "g:\". The code follows with a[/color][/color][/color]
note[color=blue][color=green][color=darkred]
> > > showing the line that is not working. When I look at the "s" variable[/color][/color][/color]
in[color=blue][color=green][color=darkred]
> > > 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[/color]
> > file[color=darkred]
> > > is duplicated. I suppose this is because the file content isn't[/color][/color][/color]
intended[color=blue][color=green]
> > to[color=darkred]
> > > be interpreted as a string (its binary after all). It is probably[/color][/color]
> hitting[color=green][color=darkred]
> > > some unfriendly bytes that it can't interpret for string operations[/color][/color][/color]
like[color=blue][color=green][color=darkred]
> > > Replace. If that's the case, maybe I need to interact with it a[/color][/color]
> character[color=green]
> > at[color=darkred]
> > > a time, although I'm not sure how I would do a replace that way. Any[/color][/color]
> ideas[color=green][color=darkred]
> > > 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,[/color]
> > FileAccess.Write)[color=darkred]
> > > Dim sw As New StreamWriter(fs2, System.Text.Encoding.UTF8)
> > > sw.Write(s)
> > >
> > > sw.Close()
> > > fs2.Close()
> > > End Sub
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]