By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,165 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,165 IT Pros & Developers. It's quick & easy.

Counting lines in a text file

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
Thanks for responses..very helpful

Feb 16 '06 #7

P: 1
bek
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.