[RESOLVED] FileStream, ReadBytes with interval
I'm trying to read bytes with 1200 interval for 100 times, but I can't get it working. This is my code:
Code:
Try
Dim fr As FileStream = File.OpenRead(OpenFileDialog1.FileName)
Dim data(fr.Length) As Byte
Using reader As New BinaryReader(fr)
Const START As Long = 4896
fr.Seek(START, SeekOrigin.Begin)
data = reader.ReadBytes(data.Length)
End Using
ListBox1.Items.Add(System.Text.Encoding.UTF8.GetString(data))
fr.Close()
Catch
End Try
I need to read from offset 4896, then from 6096, then from 7296 for 100 times and for each interval add to the listbox. who can help me? Thanks
Re: FileStream, ReadBytes with interval
I dont get it.....is that code executed by a timer ? What exactly is it doing that its not supposed to ?
Need more information.
Re: FileStream, ReadBytes with interval
No this code is executed by a button, and I need to read data from offset 4896, then 6096, then 7296... for 100 times. The interval is offset 1200.
Example for read data to offset 4896 and then to 6096 and add those 2 items to listbox:
Code:
Button1.Click........
Try
Dim fr As FileStream = File.OpenRead(OpenFileDialog1.FileName)
Dim data(fr.Length) As Byte
Using reader As New BinaryReader(fr)
Const START As Long = 4896
fr.Seek(START, SeekOrigin.Begin)
data = reader.ReadBytes(data.Length)
End Using
ListBox1.Items.Add(System.Text.Encoding.UTF8.GetString(data))
fr.Close()
Catch
End TryTry
Dim fr As FileStream = File.OpenRead(OpenFileDialog1.FileName)
Dim data(fr.Length) As Byte
Using reader As New BinaryReader(fr)
Const START As Long = 6096
fr.Seek(START, SeekOrigin.Begin)
data = reader.ReadBytes(data.Length)
End Using
ListBox1.Items.Add(System.Text.Encoding.UTF8.GetString(data))
fr.Close()
Catch
End Try
Regular offset interval = 1200
But it's hard to do if I want to read 100 datas so I'm looking for a fast way
Re: FileStream, ReadBytes with interval
Did soem changes to make a 100* Times Loop to step through the Stream. No checking for EndOfStream
Is that assingment to a new ListBox1.Item correct? (Only the last value of Data, not the previous ones?)
Vb.Net Code:
Try
Dim fr As FileStream = File.OpenRead("test.txt")
Dim data(fr.Length) As Byte
Using reader As New BinaryReader(fr)
Dim START As Long = 4896
Dim i As Integer = 0
Do
fr.Seek(START, SeekOrigin.Begin)
data = reader.ReadBytes(data.Length)
START += 1200
i += 1
Loop Until i = 100 ' do loop is done 100 times!
fr.Seek(START, SeekOrigin.Begin)
data = reader.ReadBytes(data.Length)
End Using
ListBox1.Items.Add(System.Text.Encoding.UTF8.GetString(data))
fr.Close()
Catch
End Try
Re: FileStream, ReadBytes with interval
Quote:
Originally Posted by
opus
Did soem changes to make a 100* Times Loop to step through the Stream. No checking for EndOfStream
Is that assingment to a new ListBox1.Item correct? (Only the last value of Data, not the previous ones?)
Vb.Net Code:
Try
Dim fr As FileStream = File.OpenRead("test.txt")
Dim data(fr.Length) As Byte
Using reader As New BinaryReader(fr)
Dim START As Long = 4896
Dim i As Integer = 0
Do
fr.Seek(START, SeekOrigin.Begin)
data = reader.ReadBytes(data.Length)
START += 1200
i += 1
Loop Until i = 100 ' do loop is done 100 times!
fr.Seek(START, SeekOrigin.Begin)
data = reader.ReadBytes(data.Length)
End Using
ListBox1.Items.Add(System.Text.Encoding.UTF8.GetString(data))
fr.Close()
Catch
End Try
Thanks for the code, but I need to read also previews data. Anyway I've made some changes and I've resolved, thank you very much!
This is the working code:
Code:
Try
Dim fr As FileStream = File.OpenRead(OpenFileDialog1.FileName)
Dim data(fr.Length) As Byte
Using reader As New BinaryReader(fr)
Dim START As Long = 4896
Dim i As Integer = 0
Do
START += 1200
i += 1
fr.Seek(START, SeekOrigin.Begin)
data = reader.ReadBytes(data.Length)
ListBox1.Items.Add(System.Text.Encoding.UTF8.GetString(data))
Loop Until i = 100 ' do loop is done 100 times!
End Using
fr.Close()
Catch
End Try
Re: FileStream, ReadBytes with interval
Quote:
Originally Posted by
rossi_4656
Thanks for the code, but I need to read also previews data. Anyway I've made some changes and I've resolved, thank you very much!
This is the working code:
Code:
Try
Dim fr As FileStream = File.OpenRead(OpenFileDialog1.FileName)
Dim data(fr.Length) As Byte
Using reader As New BinaryReader(fr)
Dim START As Long = 4896
Dim i As Integer = 0
Do
START += 1200
i += 1
fr.Seek(START, SeekOrigin.Begin)
data = reader.ReadBytes(data.Length)
ListBox1.Items.Add(System.Text.Encoding.UTF8.GetString(data))
Loop Until i = 100 ' do loop is done 100 times!
End Using
fr.Close()
Catch
End Try
In my humble opinion you are not reading any PREVIOUS data with your code, you start to read from byte# 6096!
Re: FileStream, ReadBytes with interval
Quote:
Originally Posted by
opus
In my humble opinion you are not reading any PREVIOUS data with your code, you start to read from byte# 6096!
Well, I've tryed the code and it's working :D. Anyway my datas are hex values, maybe this is the reason that the code it's working :ehh:
Example data at offset 6096:
Code:
56 41 4C 55 45 20 31 00 00
So the program read from offset 6096 to 7002, then loop to offset 7296 (+1200)