views:

349

answers:

2

So my program needs to go through a plain text file line by line essentially:

Read line 1: Do commands loop Read line2: Do Commands loop etc until its done with the entire file does anyone know any good coding examples for this, all the tutorials seem to show open and writing/reading textfiles but nothing on how to do it line by line.

A: 

You could do it like this:

Using f As System.IO.FileStream = System.IO.File.OpenRead("somefile.txt")
 Using s As System.IO.StreamReader = New System.IO.StreamReader(f)
  While Not s.EndOfStream
   Dim line As String = s.ReadLine

   'put you line processing code here

  End While
 End Using
End Using
Sani Huttunen
hmm getting syntax error on this one :( basically all coming up red when copied/pasted
I've tested it and it works.You should put the above code inside a function.
Sani Huttunen
Yeah I did that... hmm
+1  A: 
For Each line As String In System.IO.File.ReadAllLines("file.txt")
  ' Do Something'
Next
Andrew Kennan