Results 1 to 13 of 13

Thread: [RESOLVED] Reading Line X to the last line - txt file

  1. #1

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Resolved [RESOLVED] Reading Line X to the last line - txt file

    Hello Guys .

    I have a txt file which has unspecific number of Lines in it. I need to get lines From X (For e.g. the seventh line) to the last line.

    I would be grateful if you help me with this

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Reading Line X to the last line - txt file

    vb.net Code:
    1. Private Function ReadLinesFromIndex(filePath As String, firstLineIndex As Integer) As String()
    2.     Return IO.File.ReadLines(filePath).Skip(firstLineIndex).ToArray()
    3. End Function
    Note that indexes are zero-based, so if you wanted to read from the seventh line then you'd specify an index of 6.

    Also note that this does still read every line but simply discards those before the one you want. There's really no way to do it without reading those other lines.

  3. #3

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: Reading Line X to the last line - txt file

    Quote Originally Posted by jmcilhinney View Post
    vb.net Code:
    1. Private Function ReadLinesFromIndex(filePath As String, firstLineIndex As Integer) As String()
    2.     Return IO.File.ReadLines(filePath).Skip(firstLineIndex).ToArray()
    3. End Function
    Note that indexes are zero-based, so if you wanted to read from the seventh line then you'd specify an index of 6.

    Also note that this does still read every line but simply discards those before the one you want. There's really no way to do it without reading those other lines.
    What would be the output data type ? String ?

    Consider this :

    Dim LinesIWant as string


    If I use ReadLinesFromIndex("C:\1.txt", 6) , what would be LinesIWant ?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Reading Line X to the last line - txt file

    I suggest that you read the code a bit more carefully. Part of a function declaration is the return type.

  5. #5

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: Reading Line X to the last line - txt file

    Quote Originally Posted by jmcilhinney View Post
    I suggest that you read the code a bit more carefully. Part of a function declaration is the return type.
    I read carefully , u didn't pay enough attention to the issue : Your code output is array , selected lines as strings in it. So I should do like this :

    Dim readText() As String = ReadLinesFromIndex("C:\1.txt", 7)

    Dim s As String
    For Each s In readText
    Console.WriteLine(s)
    Next

    There will be errors concerning path issues like illegal characters and not supported type of file (I tested with txt format)

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Reading Line X to the last line - txt file

    Ok, is that a question? You can certainly get errors if you supply invalid data, so what's the question?
    My usual boring signature: Nothing

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Reading Line X to the last line - txt file

    Quote Originally Posted by GlowingVB View Post
    u didn't pay enough attention to the issue
    Exactly what issue is it that I didn't pay enough attention to? You asked for something and I provided code that did it. If there's some other issue that you think I need to pay attention to then not mentioning it is probably not the way to draw my attention to it.
    Quote Originally Posted by GlowingVB View Post
    There will be errors concerning path issues like illegal characters and not supported type of file (I tested with txt format)
    Sure, but I'm not going to provide full exception handling in every example method I write on a forum. I have no idea what code such a method will be called from so I have no idea what validation or error handling might be required. Maybe File.Exists will be called right before this method. Maybe there's a Catch block in the calling method. I don't know and, frankly, I don't care. I have provided code that addresses the issue described. How to write validation and/or exception handling code is a topic in itself that I'm not going to address in every answer I provide.

    Then again, maybe you're just trying to deflect attention from the fact you didn't bother to read the code provided properly by implying that I did something wrong too. If so then perhaps that makes you feel better but it's not the best way to get people to help you again in the future. Maybe just read the code they provide properly and then there's no issue at all.

  8. #8

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: Reading Line X to the last line - txt file

    That's motivation for me If one doesn't care about the issues I may encounter testing a piece of code. I came up with (another) solution, of mine :

    Code:
    Dim fileLines As String() = IO.File.ReadAllLines("C:\test.txt")
    
    Dim X as integer  ' Number of first lines that aren't into subject
    
    Dim LastLines As Integer = fileLines.Length - X   ' Number of lines after the X(th) line
    
    Dim SelectedLines as string  ' lines after the x(th) line of file to the last
    
                Dim i As Integer
                For i = 1 To lastlines
    
                    SelectedLines =  fileLines(i + (x - 1))  & vbNewLine & SelectedLines 
    
                Next
    Last edited by GlowingVB; Feb 25th, 2018 at 12:47 AM.

  9. #9

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: Reading Line X to the last line - txt file

    Quote Originally Posted by Shaggy Hiker View Post
    Ok, is that a question? You can certainly get errors if you supply invalid data, so what's the question?
    IDK

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [RESOLVED] Reading Line X to the last line - txt file

    Why are you calling Val() on fileLines.Length? The Length will return an integer, which is what you want. Val will turn that integer into a Double, which you don't want. Since you have Option Strict OFF, the compiler will implicitly convert the Double back to an Integer, which works, but it's a waste of time. Get rid of the Val and it will work a bit better, though you'll never see the difference.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: [RESOLVED] Reading Line X to the last line - txt file

    Quote Originally Posted by Shaggy Hiker View Post
    Why are you calling Val() on fileLines.Length? The Length will return an integer, which is what you want. Val will turn that integer into a Double, which you don't want. Since you have Option Strict OFF, the compiler will implicitly convert the Double back to an Integer, which works, but it's a waste of time. Get rid of the Val and it will work a bit better, though you'll never see the difference.
    Thanks buddy, I edited the code.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: [RESOLVED] Reading Line X to the last line - txt file

    If what you wanted was a single String containing that text then that's what you should have said. Maybe that's what you were trying to say in post #3 but I've reread it and I still don't see that. Regardless, you can make a small modification to my code to get that:
    vb.net Code:
    1. Private Function ReadLinesFromIndex(filePath As String, firstLineIndex As Integer) As String
    2.     Return String.Join(Environment.NewLine, IO.File.ReadLines(filePath).Skip(firstLineIndex))
    3. End Function
    There is a lot of different ways you could achieve the aim whether it's String or String() that you want. Unless the files will be fairly small though, I'd suggest that string concatenation using the & operator is not among the best options. Here are two more options:
    vb.net Code:
    1. Private Function ReadLinesFromIndex(filePath As String, firstLineIndex As Integer) As String
    2.     Dim allLines = IO.File.ReadAllLines(filePath)
    3.  
    4.     Return String.Join(Environment.NewLine, allLines, firstLineIndex, allLines.Length - firstLineIndex)
    5. End Function
    6.  
    7. Private Function ReadLinesFromIndex(filePath As String, firstLineIndex As Integer) As String
    8.     Dim builder As New StringBuilder
    9.  
    10.     Using reader As New IO.StreamReader(filePath)
    11.         Dim lineIndex = 0
    12.         Dim line = reader.ReadLine()
    13.  
    14.         Do Until line Is Nothing
    15.             If lineIndex >= firstLineIndex Then
    16.                 builder.AppendLine(line)
    17.             End If
    18.  
    19.             lineIndex += 1
    20.             line = reader.ReadLine()
    21.         Loop
    22.     End Using
    23.  
    24.     Return builder.ToString()
    25. End Function

  13. #13

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: [RESOLVED] Reading Line X to the last line - txt file

    Quote Originally Posted by jmcilhinney View Post
    If what you wanted was a single String containing that text then that's what you should have said. Maybe that's what you were trying to say in post #3 but I've reread it and I still don't see that. Regardless, you can make a small modification to my code to get that:
    vb.net Code:
    1. Private Function ReadLinesFromIndex(filePath As String, firstLineIndex As Integer) As String
    2.     Return String.Join(Environment.NewLine, IO.File.ReadLines(filePath).Skip(firstLineIndex))
    3. End Function
    There is a lot of different ways you could achieve the aim whether it's String or String() that you want. Unless the files will be fairly small though, I'd suggest that string concatenation using the & operator is not among the best options. Here are two more options:
    vb.net Code:
    1. Private Function ReadLinesFromIndex(filePath As String, firstLineIndex As Integer) As String
    2.     Dim allLines = IO.File.ReadAllLines(filePath)
    3.  
    4.     Return String.Join(Environment.NewLine, allLines, firstLineIndex, allLines.Length - firstLineIndex)
    5. End Function
    6.  
    7. Private Function ReadLinesFromIndex(filePath As String, firstLineIndex As Integer) As String
    8.     Dim builder As New StringBuilder
    9.  
    10.     Using reader As New IO.StreamReader(filePath)
    11.         Dim lineIndex = 0
    12.         Dim line = reader.ReadLine()
    13.  
    14.         Do Until line Is Nothing
    15.             If lineIndex >= firstLineIndex Then
    16.                 builder.AppendLine(line)
    17.             End If
    18.  
    19.             lineIndex += 1
    20.             line = reader.ReadLine()
    21.         Loop
    22.     End Using
    23.  
    24.     Return builder.ToString()
    25. End Function
    That's ok, no problem, Thanks for the code

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