|
-
Nov 14th, 2010, 05:01 AM
#1
Thread Starter
Addicted Member
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
-
Nov 14th, 2010, 05:18 AM
#2
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.
-
Nov 14th, 2010, 05:21 AM
#3
Fanatic Member
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 
-
Nov 14th, 2010, 05:23 AM
#4
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 
-
Nov 14th, 2010, 05:52 AM
#5
Re: reading a text file
 Originally Posted by J-Deezy
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.
-
Nov 14th, 2010, 11:29 PM
#6
Fanatic Member
Re: reading a text file
 Originally Posted by jmcilhinney
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 
-
Nov 14th, 2010, 11:39 PM
#7
Re: reading a text file
 Originally Posted by J-Deezy
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:
<SecuritySafeCritical> _ Public Shared Function ReadAllLines(ByVal path As String) As String() If (path Is Nothing) Then Throw New ArgumentNullException("path") End If If (path.Length = 0) Then Throw New ArgumentException(Environment.GetResourceString("Argument_EmptyPath")) End If Return File.InternalReadAllLines(path, Encoding.UTF8) End Function
and here's the implementation of InternalReadAllLines that it calls:
vb.net Code:
Private Shared Function InternalReadAllLines(ByVal path As String, ByVal encoding As Encoding) As String() Dim list As New List(Of String) Using reader As StreamReader = New StreamReader(path, encoding) Dim str As String Do While (Not str = reader.ReadLine Is Nothing) list.Add(str) Loop End Using Return list.ToArray 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.
-
Nov 16th, 2010, 12:57 AM
#8
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|