Binary Reading Problem

vish707

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

I am trying to read a binary file using binary reader class. Can someone guide me as to how do i read a specific value in the binary file in terms of defining its position?

For example:

If the total number of bytes are 50 in the file. How do i extract the value of the 25th byte or 40th byte etc. I can read the entire file at one but would like to read 25th byte in the file.

Please help.
 
Instead you can read the Stream directly, set its Position then use the ReadByte method.
 
Adding bytes from a file

I have a problem with my code:

I would like to add all the bytes and display the sum in the textbox. I am using the following code:

'I am positioning to the first byte in the file

r.BaseStream.Position = 0

dim i as integer
dim z() as byte

'fs.length-1 will count upto the last byte

for i = 0 to fs.length-1
z += r.readline(i)
next

textbox1.text=z

THIS GIVES ME AN ERROR. PLS HELP
 
r.readline(i) ???
VB.NET:
Dim sum As Long = 0
Dim fs As IO.FileStream = IO.File.OpenRead("filepath")
For i As Integer = 0 To fs.Length - 1
    sum += fs.ReadByte
Next
fs.Close()
TextBox1.Text = sum.ToString
Set fs.Position if you need to set it.
 
Well it was r.readbyte (mistyped it here)

If you point to the start of the code you want to show us, hold the left mouse button down and point to the end of teh code, then press Ctrl+C.. you can come here and press Ctrl+V to show us the exact code you have. No mis-typings or anything. Pretty cool trick actually..

PS; if youre using the wysiwyg editor and it mangles your code up with coloured highlighting, use this button:
switchmode.gif
to switch editor mode to simple..
 
Back
Top