views:

160

answers:

4

i want to know how much lines i have in my file, how can i do it in a simple way( i mean, not to go through on all over the file and count each line) is there a command for that?

+8  A: 

Well:

int lines = File.ReadAllLines(path).Length;

is fairly simple, but not very efficient for huge text files. I'd probably use a TextReader in most cases, to avoid excessive buffering:

int lines = 0;
using (TextReader reader = File.OpenText(path)) {
    while (reader.ReadLine() != null) { lines++; }
}
Marc Gravell
tanks!!!!by the way, i used this for a file with 65500 lines, so i think it wors even for bug files...
+4  A: 

Any "function" you call is going to essentially do the same thing -- go line by line and count the number of new line characters.

If you want to decieve yourself into making something seem more "crisp" a regex expression to count the number of new line characters would do the trick.

George
Once you've read the whole file in - not really feasible if it's big. Or you do it per line...oh wait... ;-)
Wim Hollebrandse
Should we tell the OP that this will still read the whole file? ;) I suppose if there was metadata attached to the file that tracked the number of lines (it would probably only apply to text files) then you could just read that. But I don't think such a thing exists.
FrustratedWithFormsDesigner
A: 

One way or another (Regex, FileStream etc.) something, somewhere will have to go through the newlines.

Wim Hollebrandse
+1  A: 

Depends on the length of the line...usually the fastest way is to do this using this simple formula - get the length of the file in bytes and divided by the length of the line, for example:

const int LINE_LENGTH = 80;
System.IO.FileInfo f = new System.IO.FileInfo(path);
int noLines = f.Length / LINE_LENGTH;

The above assumes the length of the line is a constant 80 characters wide...

If the length is variable, there is no other way of doing it, apart from opening the file and iterate through each line and keep track of the line count.

Edit: I just realized a much easier way after I posted a comment below.. Would it not be easier to actually use the Shell under System.Diagnostics.Process namespace to invoke grep -c "$(\r?\n?|\n)" using an argument as the path to the file in question - this would make it more flexible and can cater for any file. The only thing you would have to do is read in the output and regex it to get at the actual number of lines, for instance, using my grep that came with Borland C, I get this output

C:\Documents and Settings\Tom>grep -c "$(\r?\n?)" 273.txt
File 273.txt:
35 lines match

The output and mileage may vary, the -c command switch to grep means 'count of'

Hope this helps, Best Regards, Tom.

tommieb75
And it assumes ASCII or low-end UTF8 encoding, plus it doesn't account for the length of the newline character(s) themselves.
Marc Gravell
the lines length is veriable and long then 80 chars..thanks anyway..
Hey, who is going to actually open a file with +65k lines, count each one, and then compare it to the results of this code? I say put it out there and change it if somebody submits a bug report.
Will
Or why not do a system shell process invoking grep -c "$(\r?\n)" to count the lines? You get the drift... ;)
tommieb75