Results 1 to 7 of 7

Thread: [RESOLVED] FileStream, ReadBytes with interval

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    15

    [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
    Last edited by rossi_4656; Dec 20th, 2011 at 08:13 AM.
    Sorry for my bad english

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    15

    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
    Sorry for my bad english

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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:
    1. Try
    2.             Dim fr As FileStream = File.OpenRead("test.txt")
    3.             Dim data(fr.Length) As Byte
    4.             Using reader As New BinaryReader(fr)
    5.                 Dim START As Long = 4896
    6.                 Dim i As Integer = 0
    7.                 Do
    8.                     fr.Seek(START, SeekOrigin.Begin)
    9.                     data = reader.ReadBytes(data.Length)
    10.                     START += 1200
    11.                     i += 1
    12.                 Loop Until i = 100 ' do loop is done 100 times!
    13.                 fr.Seek(START, SeekOrigin.Begin)
    14.                 data = reader.ReadBytes(data.Length)
    15.             End Using
    16.             ListBox1.Items.Add(System.Text.Encoding.UTF8.GetString(data))
    17.             fr.Close()
    18.         Catch
    19.         End Try
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    15

    Re: FileStream, ReadBytes with interval

    Quote Originally Posted by opus View Post
    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:
    1. Try
    2.             Dim fr As FileStream = File.OpenRead("test.txt")
    3.             Dim data(fr.Length) As Byte
    4.             Using reader As New BinaryReader(fr)
    5.                 Dim START As Long = 4896
    6.                 Dim i As Integer = 0
    7.                 Do
    8.                     fr.Seek(START, SeekOrigin.Begin)
    9.                     data = reader.ReadBytes(data.Length)
    10.                     START += 1200
    11.                     i += 1
    12.                 Loop Until i = 100 ' do loop is done 100 times!
    13.                 fr.Seek(START, SeekOrigin.Begin)
    14.                 data = reader.ReadBytes(data.Length)
    15.             End Using
    16.             ListBox1.Items.Add(System.Text.Encoding.UTF8.GetString(data))
    17.             fr.Close()
    18.         Catch
    19.         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
    Sorry for my bad english

  6. #6
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: FileStream, ReadBytes with interval

    Quote Originally Posted by rossi_4656 View Post
    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!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    15

    Re: FileStream, ReadBytes with interval

    Quote Originally Posted by opus View Post
    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 . Anyway my datas are hex values, maybe this is the reason that the code it's working

    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)
    Sorry for my bad english

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width