[RESOLVED] Reading and then splitting information
I have a CSV file, comma delimited, three fields and unknown rows as this constantly changes...
I want to break apart the files, and have slightly more control over them. Basically I want to remove the first field, and then do as I please with the other two fields.
Atm I have this code:
VB Code:
Dim reader As StreadReader = File.OpenText("file.txt")
Dim line As String
Dim a() As String
Dim j As Integer
Dim Tester As String
Dim LineCount As Integer = 0
While Not (reader.ReadLine() Is Nothing)
LineCount += 1
End While
For j = 0 To LineCount
line = reader.ReadLine()
a = line.Split(",")
Tester = Tester & a(1) & ","
Tester = Tester & a(2) & vbNewLine
Next
reader.Close()
myfield.Text = Tester
Now I get an error when I load this code... Before I tried to make it work with multiple lines, it worked fine. Is anyone able to help me find the problem?
Many thanks :)
Re: Reading and then splitting information
VB Code:
While Not (reader.ReadLine() Is Nothing)
LineCount += 1
End While
For j = 0 To LineCount
line = reader.ReadLine()
You're reading the whole file and counting the lines, and then trying to read it again in the loop below it. Since you are already at the end of the file the line "line = reader.Readline()" will likely be causing the error. Look for a command to return to the start of the streamreader, or otherwise just close + reopen it before starting the loop. :)
Re: Reading and then splitting information
You try to read When there's no more lines :
VB Code:
While Not (reader.ReadLine() Is Nothing)
LineCount += 1
End While
This piece of code read your file to the end
So I suppose you get an error on this line :
VB Code:
Tester = Tester & a(2) & vbNewLine as a(2) is nothing
Maybe you should do this (I don't know if it's the best way to do it, but it works:
VB Code:
Dim reader As StreamReader = File.OpenText("file.txt")
Dim line As String
Dim a() As String
Dim Tester As String
Dim LineCount As Integer = 0
Tester =""
line = reader.ReadLine()
While Not (line Is Nothing)
a = line.Split(",")
Tester = Tester & a(1) & ","
Tester = Tester & a(2) & vbNewLine
LineCount += 1
End While
reader.Close()
myfield.Text = Tester
Re: Reading and then splitting information
OK I changed the code to this:
VB Code:
Dim reader As StreadReader = File.OpenText("file.txt")
Dim line As String
Dim a() As String
Dim j As Integer
Dim Tester As String
Dim LineCount As Integer = 0
While Not (reader.ReadLine() Is Nothing)
line = reader.ReadLine()
a = line.Split(",")
Tester = Tester & a(1) & ","
Tester = Tester & a(2) & vbNewLine
End While
reader.Close()
myfield.Text = Tester
And I am still getting the error, which is:
An unhandled exception has occured in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will be shut down immediately.
Object reference not set to an instance of an object.
Any ideas? :confused:
[EDIT]
matt3011 - I just tried your code, and it makes my application crash, no errors :sick:
Re: Reading and then splitting information
Oops I forgot 1 line and it was an endless loop :
VB Code:
Dim reader As StreamReader = File.OpenText("file.txt")
Dim line As String
Dim a() As String
Dim Tester As String
Dim LineCount As Integer = 0
Tester =""
line = reader.ReadLine()
While Not (line Is Nothing)
a = line.Split(",")
Tester = Tester & a(1) & ","
Tester = Tester & a(2) & vbNewLine
LineCount += 1
[B]line = reader.ReadLine()[/B]
End While
reader.Close()
myfield.Text = Tester
This should work now
Re: Reading and then splitting information