View Full Version : Count number of lines in a text file
eXeCuTeR
04-06-2008, 12:16 PM
Erm, I'm trying to think of a way to count the number of lines in a text file.
Well, I thought of
while( fgets()... == '\n') counter++;
am I right? BTW, does fgets exist in C#?
Any suggestions/corrections?
rags_to_riches
04-06-2008, 01:49 PM
System.IO.TextReader (http://msdn2.microsoft.com/en-us/library/system.io.textreader.aspx)
Magos
04-06-2008, 01:54 PM
If the textfile aint too big and/or you don't care about a slight performance hit you could split each line:
int NrOfLines = System.IO.File.ReadAllText("MyFile.txt").Split(new string[] { "\n" }, System.StringSplitOptions.None).Length;
TheRaven
04-30-2008, 01:46 AM
Magos is the man!
Been wondering about the address of text streams and lines in files with regarding the \n character for some time myself. Very simple and to the point. Excellent!!
Thank you very much.
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.