Reputation: 3
I can not figure this out. My project is to take a text file, read it into my program, change the values of it, then rewrite it back to that text file. The program will work the first time, but then the second time I run it the for-next loop I use to read from the file says that there is no text on the second line. The code just stops after the first loop and brings up the application. Anyone have an idea what is going wrong?
Here is the StreamReader loop I use:
Dim inventoryReader As New StreamReader("Inventory.txt")
Dim line As String
Dim inventorycounter As Decimal = 0
Do Until inventoryReader.EndOfStream
line = inventoryReader.ReadLine()
inventoryinfo = line.Split(ControlChars.Tab)
inventory(inventorycounter, 0) = inventoryinfo(0)
inventory(inventorycounter, 1) = inventoryinfo(1)
inventorycounter += 1
Loop
inventoryreader.close()
This is the StreamWriter loop I use:
Dim output As New StreamWriter("Inventory.txt")
For outputcounter As Integer = 0 To 16
output.WriteLine(inventory(outputcounter, 0) & ControlChars.Tab & inventory(outputcounter, 1) & ControlChars.Cr)
Next
output.WriteLine(inventory(17, 0) & ControlChars.Tab & inventory(17, 1))
output.Close()
I think the problem is that the program doesn't read the ControlChars.Cr the second time, but I have tried CrLf, Lf, and Nextline and nothing had worked.
Thanks for any and all help guys!
Upvotes: 0
Views: 143
Reputation: 942010
That's not correct, your program writes Cr + Cr + Lf to the text file. Confuzzling StreamReader, it will treat the first Cr is a line terminator. So you'll read an empty line, kaboom when you index inventoryinfo
Remove ControlChars.Cr from the code. StreamWriter.WriteLine() doesn't need any help, it already writes the proper line terminator.
Upvotes: 1