Hello!
I'm trying to read the last five lines of a csv file and I'm encountering problems with the last one. I'm using the following code:
fName = "c:\temp\filexx.csv"
If System.IO.File.Exists(fName) = True Then
Dim tmpstream As StreamReader = File.OpenText(fName)
Dim lineCount = File.ReadAllLines(fName).Length
TextLine = tmpstream.ReadToEnd().Split(Environment.NewLine)
SplitLine = Split(TextLine(0), ",")
For X = 0 To 4
strline = TextLine(lineCount - X - 1).Split(",") 'trying to read the last 5 lines
For Y = 0 To 5 '6 columns
P1(X, Y) = strline(Y) 'P1 is the array where I want the data
Next
Next
tmpstream.Close() 'close reader
Else
MsgBox("File doesn't exist")
End If
This way the program reads the five next to last lines. I've I changed strline = TextLine(lineCount - X - 1).Split(",") to strline = TextLine(lineCount - X).Split(",") and the program breaks at P1(X, Y) = strline(Y) , because index out of the bounds of the array strline(Y)
I appreciate your understanding and your help.
I'm trying to read the last five lines of a csv file and I'm encountering problems with the last one. I'm using the following code:
fName = "c:\temp\filexx.csv"
If System.IO.File.Exists(fName) = True Then
Dim tmpstream As StreamReader = File.OpenText(fName)
Dim lineCount = File.ReadAllLines(fName).Length
TextLine = tmpstream.ReadToEnd().Split(Environment.NewLine)
SplitLine = Split(TextLine(0), ",")
For X = 0 To 4
strline = TextLine(lineCount - X - 1).Split(",") 'trying to read the last 5 lines
For Y = 0 To 5 '6 columns
P1(X, Y) = strline(Y) 'P1 is the array where I want the data
Next
Next
tmpstream.Close() 'close reader
Else
MsgBox("File doesn't exist")
End If
This way the program reads the five next to last lines. I've I changed strline = TextLine(lineCount - X - 1).Split(",") to strline = TextLine(lineCount - X).Split(",") and the program breaks at P1(X, Y) = strline(Y) , because index out of the bounds of the array strline(Y)
I appreciate your understanding and your help.
Last edited: