Results 1 to 5 of 5

Thread: How do I get StreamReader.ReadBlock to start at a specific line

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    6

    Question How do I get StreamReader.ReadBlock to start at a specific line

    Hello everyone. I want streamreader.Readblock to start reading a file from a specific line, but I need some help with this. So far I have have the following example code:

    Code:
            Dim lineposition As Integer = 20
            Dim buffer As Char() = ""
            Dim count As Integer = 256
            Dim dataReader As New StreamReader("C:\lines.txt")
            For i = lineposition To 100
    
                dataReader.ReadBlock(buffer, i, count)
                'show data 
                MsgBox("Success " & buffer.ToString)
    
            Next i
            dataReader.Close()
    but I recieve the error: "Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection."

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: How do I get StreamReader.ReadBlock to start at a specific line

    you need to initialize your buffer array, + you also need to be sure that StartPosition + 256 isn't greater than the length of the file:

    vb Code:
    1. Dim buffer(255) As Char
    2. Dim dataReader As New IO.StreamReader("C:\lines.txt")
    3. dataReader.ReadBlock(buffer, StartPosition, 256)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do I get StreamReader.ReadBlock to start at a specific line

    ReadBlock is going to start reading at a specific character offset from the current position. You can't tell it to start reading at a specific line unless you know the character index at which that line starts. If you want start reading a file at the 20th line and you don't know exactly how long those lines are, you have no choice but to simply read all lines and discard the ones you don't need.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    6

    Re: How do I get StreamReader.ReadBlock to start at a specific line

    Quote Originally Posted by jmcilhinney View Post
    If you want start reading a file at the 20th line and you don't know exactly how long those lines are, you have no choice but to simply read all lines and discard the ones you don't need.
    I guess I can't use readblock then, thanks.

  5. #5
    Lively Member
    Join Date
    Jan 2007
    Posts
    96

    Re: How do I get StreamReader.ReadBlock to start at a specific line

    Heres a function that I wrote a while back that will allow you to read text from a file with parameters for maxLines / startLine. Hope this helps.

    Code:
        Public Function ReadFile(ByVal Path As String, _
                                 Optional ByVal MaxLines As Integer = 0, _
                                 Optional ByVal StartLine As Integer = 0) As String
            Try
                If Not System.IO.File.Exists(Path) Then Return String.Empty ' Make sure the file exists
                ' Create our counter (how many lines have been read) and currentline (self explanitory) variables
                Dim Count As Integer = 0 : Dim CurrentLine As Integer = 0
                Dim sb As New System.Text.StringBuilder ' Create a new stringbuilder
                Dim sr As New System.IO.StreamReader(New System.IO.FileStream(Path, System.IO.FileMode.Open, System.IO.FileAccess.Read)) ' Read file into stream
                While Not sr.EndOfStream ' Loop while we aren't at the end of the stream
                    ' If maxlines is not unlimited (0) and number of lines exceeds the maxlines exit the loop
                    If Not MaxLines = 0 AndAlso Count >= MaxLines Then Exit While
                    Dim Line As String = sr.ReadLine ' Read current line of stream
                    If Not String.IsNullOrEmpty(Line) Then ' Make sure that the line is not an empty string
                        If Not StartLine = 0 Then ' Check if we need to monitor for the line to start reading at
                            If CurrentLine >= StartLine Then ' The current line is equal to or greater than the startline
                                sb.AppendLine(Line) ' Add line to string builder
                                Count += 1 ' Update added lines counter
                            End If
                        Else
                            sb.AppendLine(Line) ' Add line to string builder
                            Count += 1 ' Update added lines counter
                        End If
                    End If
                    CurrentLine += 1 ' Update current line counter
                End While
                sr.Close() ' Close stream reader
                ' Trim the contents of the string builder (so there is no blank trailing line) and return
                Return sb.ToString.Trim
            Catch ex As Exception
                Debug.Print(ex.ToString) ' Debug the exception so we can see what went wrong
                Return String.Empty ' Return empty string due to exception
            End Try
        End Function
    Last edited by youthbytes; Jan 4th, 2010 at 03:10 PM.

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