No. of lines in a text file?

A

Amjad

How can I determine the total number of lines in a text
file that contains streams of text data, without going
into a loop that reads the lines one by one?

Also, I use the following code to read each line and
proceed to the next one:
'****************************************
Dim sr as StreamReader = New StreamReader("C:\MyFile.TXT")
Dim line as String = sr.ReadLine()
'****************************************
How do I point the reference to line number 75 in the
text file for example without reading the lines one by
one in a loop? And how do I read the last line in the
text file directly?

Thanks
Amjad
 
M

Mythran

Amjad said:
How can I determine the total number of lines in a text
file that contains streams of text data, without going
into a loop that reads the lines one by one?

Also, I use the following code to read each line and
proceed to the next one:
'****************************************
Dim sr as StreamReader = New StreamReader("C:\MyFile.TXT")
Dim line as String = sr.ReadLine()
'****************************************
How do I point the reference to line number 75 in the
text file for example without reading the lines one by
one in a loop? And how do I read the last line in the
text file directly?

Thanks
Amjad

ReadAll method.

Split to an array using vbNewLine or vbCrLf as the delimiter.

ArrayName.GetUpperBound(0) + 1 = Number of Lines In File

Mythran
 
H

Herfried K. Wagner [MVP]

* "Amjad said:
How can I determine the total number of lines in a text
file that contains streams of text data, without going
into a loop that reads the lines one by one?

Also, I use the following code to read each line and
proceed to the next one:
'****************************************
Dim sr as StreamReader = New StreamReader("C:\MyFile.TXT")
Dim line as String = sr.ReadLine()
'****************************************
How do I point the reference to line number 75 in the
text file for example without reading the lines one by
one in a loop? And how do I read the last line in the
text file directly?

There is no better way because there is nowhere stored where line number
n starts. You may want to maintain a separate index file, but notice
that all start indices of succeeding lines will change when inserting a
longer/shorter line.
 
A

Amjad

Hi Cor,
Yes I can, if I pointed to the bottom of the glass and
made a little hole there!

Amjad
 
C

Cor

Hi Amjad,
Yes I can, if I pointed to the bottom of the glass and
made a little hole there!

That was an answer I did wanted to reply,

Because I did wanted to answer is it worth that much work for such a simple
thing

Cor
 
S

Simon Verona

surely this is a function of the glass object?? I presume you need to
implement a drinkfrombottom() method?

Regards
Simon
 
M

Mike Bulava

Alright now I think we all need to go home and have a drink, but remember
drink it from the bottom of the glass and give a detailed explination of how
you did it after the holiday.. :)

Merry Christmas..
 

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

Top