take a look at the io.filestream class...
Dim
sz As Integer = 100Dim sr As New IO.FileStream("Path", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None, sz, IO.FileOptions.RandomAccess)Dim Buffer(sz) As ByteDim Offset As Integer = 0sr.Read(Buffer, Offset, sz)The example I gave above would read the first 100 bytes...then you could do
Array
.Clear(Buffer, 0, Buffer.Length)sr.Read(Buffer, 100, 2)to read the next 2 bytes
But I need a way to check the characters as they're being read in. The character string that I'm seeking could be anywhere in the file, not just at the 100th byte. So how do I accomplish that?
Take some time and read up on the filestream class it has methods that allow you to access a file any way you need to... even byte by byte...which sounds like what you need to do while keeping track of the number of bytes read....give it a shot ...write some code and then come back and post the code you are having problems with![]()
public override int ReadByte()
Member of System.IO.FileStream
Summary:
Reads a byte from the file and advances the read position one byte.
Return Values:
The byte cast to an int, or -1 if reading from the end of the stream.
Exceptions:
System.ObjectDisposedException: The current stream is closed.
System.NotSupportedException: The current stream does not support reading.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' Set # of display characters here
Dim CharBytesAfterFindToShow As Integer = 4
Dim result As String = ""
Dim place As Integer = 0
' Plug in the binary file here
Dim buffer() As Byte = System.IO.File.ReadAllBytes("c:\goodoval.jpg")
' Yes, there are random strings like 'NERK'
' embedded in some large binary files, but
' probably not any of yours.
' Plug in the search string here
Dim searchstr As String = "NERK"
'Load the search string into a byte array
Dim look3(searchstr.Length - 1) As Byte
For x As Integer = 0 To searchstr.Length - 1
look3(x) = CByte(Microsoft.VisualBasic.Asc(searchstr(x)))
Next
' Search
Do While place <> -1
place = Array.IndexOf(buffer, look3(0), place, buffer.Length - place - 1)
If place = -1 Then
Exit Do
End If
Try
For x As Integer = 1 To searchstr.Length - 1
If Array.IndexOf(buffer, look3(x), place + x, 1) <> -1 Then
Else
place += 1
Exit Try
End If
Next
MsgBox("match found at position " & place.ToString)
While place + searchstr.Length < buffer.Length
result += Chr(buffer(place + searchstr.Length))
place += 1
If result.Length = CharBytesAfterFindToShow Then
Exit While
End If
End While
If result = "" Or result = vbCrLf Then
result = "No characters after match"
End If
Exit Do
Catch ex As Exception
Exit Do
End Try
If place >= buffer.Length - 1 - searchstr.Length Then
Exit Do
End If
Loop
MsgBox(result)
End Sub
End Class