Results 1 to 8 of 8

Thread: reading a text file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2010
    Posts
    137

    reading a text file

    hi all i want to put a function that reads a text file and put it in textbox1
    i used this code to read the last line
    Code:
    Textbox1.text = Io.file.readalllines("C:\c.txt").last
    but i want it to read the third line
    how can i do it
    pls help
    thx in advance

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

    Re: reading a text file

    You could do ReadAllLines(...).Skip(2).First() but that is a bit inefficient, especially if it's a large file. You'd do better to create a StreamReader and call ReadLine three times, discarding the first two results.
    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

  3. #3
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: reading a text file

    What about simply calling the third index?

    Code:
    Dim thirdLine As String = IO.File.ReadAllLines("C:\c.txt")(2)
    I think that should work, or would that be inefficient?
    If I helped you out, please take the time to rate me

  4. #4
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: reading a text file

    how about the old fusion way
    Code:
    Dim s As String() = System.IO.File.ReadAllLines("C:\c.txt)
            TextBox1.Text = s(2)
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

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

    Re: reading a text file

    Quote Originally Posted by J-Deezy View Post
    I think that should work, or would that be inefficient?
    It would be inefficient for the same reason: calling ReadAllLines requires reading the entire file. There's no point reading every line if you only need to read the first three. It's not a big deal if the file is small but the bigger the file, the more inefficient it becomes.
    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

  6. #6
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: reading a text file

    Quote Originally Posted by jmcilhinney View Post
    It would be inefficient for the same reason: calling ReadAllLines requires reading the entire file. There's no point reading every line if you only need to read the first three. It's not a big deal if the file is small but the bigger the file, the more inefficient it becomes.
    Ah okay thanks, that makes sense. I'm not too savvy with the technical side of streamreader, but doesn't that load the whole stream before you start reading lines? Or is a stream much more efficient to load than all the lines?
    If I helped you out, please take the time to rate me

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

    Re: reading a text file

    Quote Originally Posted by J-Deezy View Post
    Ah okay thanks, that makes sense. I'm not too savvy with the technical side of streamreader, but doesn't that load the whole stream before you start reading lines? Or is a stream much more efficient to load than all the lines?
    A Stream isn't something you load, but rather something you open. The Stream isn't the data, but rather a conduit through which data can travel from a source to a destination.

    Also, you aren't avoiding using a StreamReader by calling File.ReadAllText or File.ReadAllLines anyway. You don't have to use one directly, but how do you suppose those methods read the file? They use a StreamReader internally. Here's the implementation of File.ReadAllLines:
    vb.net Code:
    1. <SecuritySafeCritical> _
    2. Public Shared Function ReadAllLines(ByVal path As String) As String()
    3.     If (path Is Nothing) Then
    4.         Throw New ArgumentNullException("path")
    5.     End If
    6.     If (path.Length = 0) Then
    7.         Throw New ArgumentException(Environment.GetResourceString("Argument_EmptyPath"))
    8.     End If
    9.     Return File.InternalReadAllLines(path, Encoding.UTF8)
    10. End Function
    and here's the implementation of InternalReadAllLines that it calls:
    vb.net Code:
    1. Private Shared Function InternalReadAllLines(ByVal path As String, ByVal encoding As Encoding) As String()
    2.     Dim list As New List(Of String)
    3.     Using reader As StreamReader = New StreamReader(path, encoding)
    4.         Dim str As String
    5.         Do While (Not str = reader.ReadLine Is Nothing)
    6.             list.Add(str)
    7.         Loop
    8.     End Using
    9.     Return list.ToArray
    10. End Function
    As you can see, a StreamReader is created and ReadLine is called. It's just more convenient to make the one simple method call if you do want all the lines in the file but, if you don't, then it's more efficient to write a little bit more code and read only what you 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

  8. #8
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: reading a text file

    Ah okay thanks for the explanation John, I'll give you some rep for that

    EDIT: apparently I can't rep you again?
    If I helped you out, please take the time to rate me

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