reading and counting lines in a text file

  • Thread starter Thread starter André Freitas
  • Start date Start date
A

André Freitas

I got the following code:

Dim vReader As StreamReader
Dim Line As Integer

vReader = File.OpenText("file here")

While Not (vReader.ReadLine() Is Nothing)
line = line + 1
End While

After this, depeding of the number of lines of the text files, i need to get
a few of them. But i already have read all lines of the text file.
So, do i realy need to open the same file again, like:

vReader.Close()
vReader = File.OpenText("same file")

.... read the lines i need.

vReader.Close()

Regards,
André
 
André Freitas said:
I got the following code:

Dim vReader As StreamReader
Dim Line As Integer

vReader = File.OpenText("file here")

While Not (vReader.ReadLine() Is Nothing)
line = line + 1
End While

After this, depeding of the number of lines of the text files, i need to
get a few of them. But i already have read all lines of the text file.
So, do i realy need to open the same file again, like:

vReader.Close()
vReader = File.OpenText("same file")

... read the lines i need.

vReader.Close()

Regards,
André

You either have to read it twice, or, if the files are small enough, use
File.ReadAllLines("some file"). This returns an array of strings
representing each line. If the count is in your criteria, then just
pull the array entry at the line number you want to use.
 
Family Tree Mike said:
You either have to read it twice, or, if the files are small enough, use
File.ReadAllLines("some file"). This returns an array of strings
representing each line. If the count is in your criteria, then just pull
the array entry at the line number you want to use.

Hi Andre
You could do as Mike suggested or you could try to move to start of the
file by setting the underlying stream position to 0.

vReader.BaseStream.Position = 0;

Cheers
Krishna
 
thx

Krishna Chytanya said:
Hi Andre
You could do as Mike suggested or you could try to move to start of the
file by setting the underlying stream position to 0.

vReader.BaseStream.Position = 0;

Cheers
Krishna
 
Back
Top