Read specified num of lines into array

cfisher440

Well-known member
Joined
Oct 11, 2005
Messages
73
Programming Experience
1-3
I would like to throw a specified number of lines from a file into an array.
I understand executing this code:
VB.NET:
Dim sr as streamReader = new streamReader("File.txt")
Dim strFile as string = sr.readtoend()
Dim strLines as string = strFile.split(ControlChars.lf)
will give that array the number of lines in the whole file, but what if I only need about 15 lines out of the file (say lines 30 - 45) in an array.
How would I do that??

Any ideas would be appreciated
 
This is what I have but it does not work

VB.NET:
Dim arrayNum as int32 = 10
Dim strLines() as string = arrayNum
Dim strObj1() as string = arrayNum
Dim strObj2() as string = arrayNum
Dim strObj3() as string = arrayNum
Dim strObj4() as string = arrayNum
Dim strObj5() as string = arrayNum
Dim strObj6() as string = arrayNum
Dim strObj7() as string = arrayNum
Dim strObj8() as string = arrayNum
Dim strObj9() as string = arrayNum
Dim strObj10() as string = arrayNum
Dim num as Integer = 0
for i as int16 = 15 to 25 'Those are the lines to read
   strLines(i) = sr.readline
   Dim strObjLines() as string = Regex.Split(strLines(i), "")
            strobj1(num) = strObjLines(2)
            strobj2(num) = strObjLines(3)
            strobj3(num) = strObjLines(4)
            strobj4 = strObjLines(5)
            strobj5 = strObjLines(6)
            strobj6 = strObjLines(7)
            strobj7 = strObjLines(8)
            strobj8 = strObjLines(9)
            strobj9 = strObjLines(10)
            num = num + 1
Next
 
Last edited:
cfisher....
not exactly sure what you have going on up there but this is how I would get a certain number of lines from a text file into a string array. In this example, I'm reading 5 lines from the file into my array and they are lines 5 - 9...

Dim sr As System.IO.StreamReader = New System.IO.StreamReader("c:\file.txt")

Dim numberOfLines As Integer = 5

Dim i As Integer
Dim junkString As String

'here i read the lines 1 to 4
For i = 1 To 4
junkString = sr.ReadLine()
Next

'create an array of the size of the number of lines
'i want to read note that since arrays start their numbering
'at 0 i want my array size to be 1 less than the number i want
Dim stringArray(numberOfLines - 1) As String

'here i will read the lines and add them to the
'array

For i = 0 To numberOfLines - 1
'make sure there is something to read
If sr.Peek >= 0 Then
'read a line from the file into the array
stringArray(i) = sr.ReadLine()
End If
Next

'this is a loop to print the contents of
'the array to a message box one item
'at a time

For i = 0 To stringArray.Length - 1
MsgBox(stringArray(i))
Next

Hope this helps
 
Thank you for the reply

I believe that basically sums it up... in the program I am creating I need to
read certain lines from the file.. it is not known when those lines are going to come or how many of those lines there are going to be. Example: the lines may start on line 25 of the text file or on line 925. and they may end on line 35 or on line 1024.
I already figured out how to find the lines (where they start, and how many there are).
I was having trouble reading the lines that I did find into an array.
I believe that will help me a lot.
Just so you know what I am doing, the idea is to read in the lines, capturing the data out of the lines
example
017 78 89 23 12:31
023 83 26 56 11:21
capture all that data from those lines and throw it into a database into it's appropriate field(for now another text file (because that is easier for testing ;)))

Thank you guys for responding to my post, I appreciate it :)
 
Back
Top