Reading NULL Terminated string from file
Is there any simple way to read a null terminated string from a binary file? The problem I have is retrieving the array of bytes making up the string.
The string itself can be variable length.
The method I'm using at the moment is to scan each byte in the file from the current position until I find the NULL character, then skipping back to where the string started, and reading the array of bytes...
Code:
'This example actually just reads a string that would be at the beginning of the file
Dim BR As System.IO.BinaryReader
BR = New System.IO.BinaryReader(System.IO.File.OpenRead("d:\temp\test.dmt")
Dim SWA As Boolean = False
Dim HeaderSize As Integer
Dim NextChar As Integer
Do While SWA = False
NextChar = BR.Read()
If NextChar = 0 Then
HeaderSize += 1
SWA = True
Else
BR.Read()
HeaderSize += 1
If HeaderSize = 256 Then SWA = True
End If
Loop
BR.BaseStream.Seek(0, IO.SeekOrigin.Begin)
Dim NTS() As Byte = BR.ReadBytes(HeaderSize)
Dim Encoder As New System.Text.ASCIIEncoding()
Dim mHeaderString As String = Encoder.GetString(NTS)