471,318 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,318 software developers and data experts.

Counting lines in a text file

Hi,

Can someone tell me the easiest way to count the number of lines in a
text file?

I can write a loop to do this but it seems cumbersome.... there must be
an easier way??

Thanks in advance

Feb 15 '06 #1
7 13310
Mark..... <MA*********@HOTMAIL.COM> wrote:
Can someone tell me the easiest way to count the number of lines in a
text file?

I can write a loop to do this but it seems cumbersome.... there must be
an easier way??


I don't see why there "must" be an easier way. Just call ReadLine on a
TextReader until it returns null, incrementing a counter each time. How
could it be much easier?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 15 '06 #2
Mark..... wrote:
Can someone tell me the easiest way to count the number of lines in a
text file?

I can write a loop to do this but it seems cumbersome.... there must
be an easier way??


Hi Mark. A loop is probably the best way to do this, and is really simple.
Because lines are of unpredictable length, without any kind of metadata
stored with the file it's impossible to determine its line count without
reading every byte. If you want to do it in the least possible code, you
could use an expression like this, at the expense of a great deal of
additional memory for large files:

new StreamReader("file.txt").ReadToEnd().Split(new char[] {'\n'}).Length

--
Derrick Coetzee, MCAD, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included code samples are subject to the terms
specified at http://www.microsoft.com/info/cpyright.htm
Feb 15 '06 #3
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
I don't see why there "must" be an easier way. Just call ReadLine on a
TextReader until it returns null, incrementing a counter each time. How
could it be much easier?


Sorry, that really wasn't meant to come over as aggressively as it did.
Sincere apologies. Put it down to lousy sinuses at the moment :(

(It's still a pretty easy way of counting lines in a file though :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 15 '06 #4
> new StreamReader("file.txt").ReadToEnd().Split(new char[] {'\n'}).Length

Hehe, I though about suggesting this, but just didn't have the guts to write
such a line of code! :-)
Feb 15 '06 #5
All the other comments should be the answer to your literal question.
However, it occured to me, are you trying to get file size? If so there IS a
better way. Take a look at the FileInfo.Length in the System.IO namespace.

--
-Demetri
"Mark....." wrote:
Hi,

Can someone tell me the easiest way to count the number of lines in a
text file?

I can write a loop to do this but it seems cumbersome.... there must be
an easier way??

Thanks in advance

Feb 15 '06 #6
Thanks for responses..very helpful

Feb 16 '06 #7
bek
1
I have the same problem, except that I need to know the number of lines in a text file so that I know what to set my maximum on my progressbar to. It seems really inefficient to count to the end to get the max, then count to the end again to process the data.

Anyone have any alternative suggestions? Seems like I read something about this many years ago and someone had come up with an answer.

Thanks,
bek
May 3 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Sam Lowry | last post: by
19 posts views Thread by Alex Vinokur | last post: by
5 posts views Thread by Anders K. Jacobsen [DK] | last post: by
5 posts views Thread by andy.lee23 | last post: by
10 posts views Thread by cj | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.