Retrieving more than one line out of a text file?
Hi!
I'm trying to retrieve line 5 - 8 from a text file, but I can't figure out how. I can retrieve a single line and I can retrieve all lines, but I can't figure out how to get those specific ones. I tried using substring but it didn't work. Any ideas?
Thank you in advance!
Re: Retrieving more than one line out of a text file?
The stream reader class reads lines sequentially so in order to get to the 5th line you need to skip the preceding four.
Alternatively you can use Binary reader, but nevertheless you will need to seek for the CRLF characters in the file to count the lines.
In some weird scenario you can read the first 1024 bytes (or as much as you want) of the file and count CRLFs in this block.
You'll need the position of the 4th one + 1 to get to the beginning of the 5th line.
Re: Retrieving more than one line out of a text file?
Would it be possible to get the line number of the item i searched for then do:
Code:
Dim lines As String() = IO.File.ReadAllLines("path here")
Dim line1 As String = lines(thelineIgot + 1)
Dim line2 As String = lines(thelineIgot + 2)
Dim line3 As String = lines(thelineIgot + 3)
Dim line4 As String = lines(thelineIgot + 4)
lineslbl.text = line1, line2, line3, line4
to get the next four lines under what I searched for?
Incase, how would I get the linenumber of the item I searched for?
Re: Retrieving more than one line out of a text file?
Only by looking through all the lines.
If you don't know exactly which particular line do you need you would need to look through all of them until you find it.
Perhaps you chose the wrong format for storing your data. Text files are simple but they're not the best when you need to index the stored information for fast search.
Re: Retrieving more than one line out of a text file?
Well, it's looking through all the lines anyway when I search, so it's not really a problem.
I wish I could change it, but it's not a decision that's up to me, it's for work.
Could you tell me how to retrieve the line number of the line my serach is on, please?
Re: Retrieving more than one line out of a text file?
Code:
Dim Lines() As String = {"This line contains One", "This line contains Two", "This line contains Three"}
For LineNum As Integer = 0 To Lines.Length() - 1
If Lines(LineNum).Contains("Two") Then
MsgBox(LineNum.ToString)
Exit For
End If
Next
Re: Retrieving more than one line out of a text file?
Thank you, cicatrix.
Quote:
{"This line contains One", "This line contains Two", "This line contains Three"}
This one confused me a bit, could you please explain a bit?
Re: Retrieving more than one line out of a text file?
This is just an example array of text lines for the sake of example.
You use your own (read from the file instead).
The { and } which may confuse you are used to initialize the array with values at the moment of its declaration. Thus, you can declare an integer array 1, 2, 3 using this line of code:
Code:
Dim Values() As Integer = {1, 2, 3}
Or, more sophisticated syntax to declare a 2x3 2D array:
Code:
Dim Values(,) = {
{ 2, 4, 6 },
{ 1, 2, 3 }
}
Re: Retrieving more than one line out of a text file?
Okay, sorry for being semi-retarded here.
This is what I use to search for the word I'm looking for and to put a part of that line into a label:
Code:
Dim test = FindStringInFile("\filepath", TextBox1.Text.Substring(0, 8))
If test Then
For Each line In IO.File.ReadAllLines("filepath")
'Line = linjenummer i vare.txt
'Vist linjenummeret inneholder scanummeret i textbox1, hent varenummer
If line.Contains(TextBox1.Text.Substring(0, 8)) Then
Label2.Text = line.Substring(3, 6)
Exit For
End If
Next
Is it possible for me to get the linenumber that line.Substring(3, 6) is on using the code you suggested?
Re: Retrieving more than one line out of a text file?
Instead of For Each loop use simple For... Next as I showed you above.
Code:
Dim Lines() As String = IO.File.ReadAllLines("filepath")
For LineNum As Integer = 0 To Lines.Length - 1
If Lines(LineNum).Contains(TextBox1.Text.Substring(0, 8)) Then
Label2.Text = line.Substring(3, 6)
MstBox(LineNum.ToString())
Exit For
End If
Next
If you still want to use For Each then you will need an independent iterator variable:
Code:
Dim LineNum As Integer = 0
For Each line In IO.File.ReadAllLines("filepath")
LineNum += 1
If line.Contains(TextBox1.Text.Substring(0, 8)) Then
Label2.Text = line.Substring(3, 6)
MsgBox(LineNum.ToString)
Exit For
End If
Next
Re: Retrieving more than one line out of a text file?
OKay thank you very much!
For some reason it skips a few lines and only takes part of them..
Re: Retrieving more than one line out of a text file?
This is your code, I only showed you how to find the line number. This code only finds a string that contains the first 8 characters from Textbox1 and puts in the Label2 6 characters of the text line from your file starting from 4th character. Then it displays the line number and exits the cycle.