reading and counting lines in a text file

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é
 
F

Family Tree Mike

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.
 
K

Krishna Chytanya

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
 
A

André Freitas

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top