Question Reading a text from a binary file, changing and saving.

Oliver Mills

New member
Joined
Apr 12, 2011
Messages
2
Location
London, United Kingdom, United Kingdom
Programming Experience
Beginner
Hi, I'm new to VB and trying to figure out how I can replace a bit of descriptive text in a binary file. I presume its binary as I cant open it with anything but a hex editor. I've changed the text (file description) in the hex editor and saved it fine but I'm unsure how I go about saving just the section of information I need. I can work out the saving bit later.
In the attached file you'll see the highlighted bit I want to edit. I need to replace the whole line or the file corrupt but I can fill it up with null characters and the numbers are just so I can work out the maximum length I can use.
I hope some of this makes sense to someone and you can kindly help me out.

Regards.
 

Attachments

  • Hex Description.png
    Hex Description.png
    43.3 KB · Views: 26
You can open the raw file using a FileStream. You can then lay a StreamReader and StreamWriter on top of that to read and write text. E.g.
VB.NET:
Dim offset As Integer 'The zero-based position in the file where the text starts.
Dim length As Integer 'The length of the text.

Using file As New IO.FileStream("file path here", IO.FileMode.Open, IO.FileAccess.ReadWrite)
    Dim chars(length) As Char

    file.Position = offset

    Using reader As New StreamReader(file, Encoding.ASCII)
        reader.ReadBlock(chars, 0, length)
    End Using

    Dim text As New String(chars)

    text = text.ToLower()
    text.CopyTo(0, chars, 0, text.Length)

    file.Position = offset

    Using writer As New StreamWriter(file, Encoding.ASCII)
        writer.Write(chars, 0, length)
    End Using
End Using
 
Back
Top