473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 13467
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Sam Lowry | last post by:
Greetings. I am trying to do something which should elementary for Perl, but I have only been able to find bits and pieces on it. When I put the bits together they do not work. Maybe I am going...
19
by: Alex Vinokur | last post by:
Is there any tool to count C-program lines except comments? Thanks, ===================================== Alex Vinokur mailto:alexvn@connect.to http://mathforum.org/library/view/10978.html...
5
by: Anders K. Jacobsen [DK] | last post by:
Hi We have a rather large asp.net project with serveral utility projects (written in C#). Is there at tool out there which can give an estimate of the total amount of code lines all projects...
1
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
4
by: Peter | last post by:
Currently I'm using the method below, is there someting more efficient?: Imports System.IO Public Class CountLine Public Shared Function CountLines(ByVal FileName As String) As Integer Dim fs...
5
by: andy.lee23 | last post by:
hi im having trouble counting lines in a text file, i have the following code int node1, node2, i; char name; float value; ifstream fin; fin.open(OpenDialog1->FileName.c_str()); i=1;
10
by: cj | last post by:
I'm writing a TCP/IP server app that will have many simultaneous connections. The main thread listens for new connections and starts a thread to handle each requested connection. These are short...
7
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27...
7
by: tiredMike | last post by:
Hi, I want to count lines in a text file in the format below: 255.000000,148.000000,0.000000,0.000000 255.000000,229.000000,0.000000,0.000000 255.000000,215.000000,0.000000,0.000000 ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.