Searching Binary file

vish707

Member
Joined
Apr 16, 2007
Messages
12
Programming Experience
Beginner
Hi everyone,

I would like to search a binary file (eg.Test.inf) for some data and get the position in return.

For example;

Suppose i have a file of 800 bytes and i would like to search for a specific byte value within that file i.e. 122 (i.e. value), how do i do it.

Plus after searching the value (if it exists), i would like to get the position in return, (of that value) for other purposes.

Can someone pls help!!!
 
VB.NET:
Dim bytes() As Byte = My.Computer.FileSystem.ReadAllBytes("file.inf")
Dim index As Integer = Array.BinarySearch(bytes, CByte(122))
if index is 0 or positive it is found. (ie any negative number means not found)
 
Hi,

See what i want to do is that i read the file using binary reader and search for a specific value within that file (eg. 122). Once its found, i want the position of that 122value within the file so that my program knows that where's that value within the file.

Pls help
 
Yes, you can read BaseStream.Position property from BinaryReader, but you will be slowing down search considerably because of more file reads and many more method calls.

Of course for very very large files it could be an issue loading the full file into memory, but it would probably be many megabytes before I would consider not doing it, and instead searching smaller buffer arrays. But I would not read one by one byte (from file/stream).
 
Back
Top