views:

11757

answers:

8

Is there an easy way to programmatically determine the number of lines within a text file?

+16  A: 

If you're not too bothered about efficiency, you can simply write:

var lineCount = File.ReadAllLines(@"C:\file.txt").Length;

For a more efficient method you could do:

var lineCount = 0;
using (var reader = File.OpenText(@"C:\file.txt"))
{
    while (reader.ReadLine() != null)
    {
        lineCount++;
    }
}

Edit: In response to questions about efficiency

The reason I said the second was more efficient was regarding memory usage, not necessarily speed. The first one loads the entire contents of the file into an array which means it must allocate at least as much memory as the size of the file. The second merely loops one line at a time so it never has to allocate more than one line's worth of memory at a time. This isn't that important for small files, but for larger files it could be an issue (if you try and find the number of lines in a 4GB file on a 32-bit system, for example, where there simply isn't enough user-mode address space to allocate an array this large).

In terms of speed I wouldn't expect there to be a lot in it. It's possible that ReadAllLines has some internal optimisations, but on the other hand it may have to allocate a massive chunk of memory. I'd guess that ReadAllLines might be faster for small files, but significantly slower for large files; though the only way to tell would be to measure it with a Stopwatch or code profiler.


Seriously belated edit: If you're using .NET 4.0 or later

The File class has a new ReadLines method which lazily enumerates lines rather than greedily reading them all into an array like ReadAllLines. So now you can have both efficiency and conciseness with:

var lineCount = File.ReadLines(@"C:\file.txt").Count();
Greg Beech
Why is the second method less performant than the first? It is not apparent from the code.
Sklivvz
My gut feel would be the first may be faster, but that's just a guess
johnc
Small note: because String is a reference type the array would be the size of the number of lines x the size of a pointer, but you're correct that it still needs to store the text, each line as a single String object.
Mike Dimmick
Mike - indeed, thanks for clarifying that. That was what I meant but looking again I realise it could potentially read badly.
Greg Beech
A: 

You could quickly read it in, and increment a counter, just use a loop to increment, doing nothing with the text.

Mitchel Sellers
+3  A: 

The easiest:

int lines = File.ReadAllLines("myfile").Length;
leppie
+1  A: 

count the carriage returns/line feeds. I believe in unicode they are still 0x000D and 0x000A respectively. that way you can be as efficient or as inefficient as you want, and decide if you have to deal with both characters or not

geocoin
+1  A: 

If by easy you mean a lines of code that are easy to decipher but per chance inefficient?

string[] lines = System.IO.File.RealAllLines($filename); int cnt = lines.Count();

That's prolly the quickest way to know how many lines.

You could also do (depending on if you are buffering it in)

for large files

while (...reads into buffer){ string[] lines = Regex.Split(buffer,System.Enviorment.NewLine); }

There are other numerous ways but one of the above are prolly what you'll go with.

+1  A: 

This would use less memory, but probably take longer

int count = 0;
string line;
TextReader reader = new StreamReader("file.txt");
while ((line = reader.ReadLine()) != null)
{
  count++;
}
reader.Close();
benPearce
A: 

You can launch the "wc.exe" executable (comes with UnixUtils and does not need installation) run as an external process. It supports different line count methods (like unix vs mac vs windows).

Sklivvz
A: 

Thanks... Cool example