How to get the count of lines in .txt file
-
How to get the count of lines in .txt file. Help.
C#
-
How to get the count of lines in .txt file. Help.
C#
I'm not sure if you can do anything but brute force it. Here's one way: // Read the file into a byte array FileStream fs = File.Open("TextFile1.txt", FileMode.Open); byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, (int)fs.Length); // int to store num of lines int numOfLines = 0; // if the file is not empty, we have at least 1 line if (bytes.Length > 0) numOfLines++; // increment the count for each newline character found foreach (byte b in bytes) if(b == '\n') numOfLines ++; ... This is just a starting point and would need to be more robust depending on the requirements. If the textfile's length is greater than int.MaxValue I suppose you'd need to read the file in sections. This solution will count all lines (even blank lines and trailing blank lines). I would be interested to see another solution. I'm sure there must be a better one. Cheers, Ian
-
How to get the count of lines in .txt file. Help.
C#
That depends on what you define as a line. There is no single method you can call to just return this value. You'd have to write a function that counts the number of lines in the file, according to the rules that you define for a "line", by reading the entire file and counting the lines up, one-by-one.
Dave Kreskowiak Microsoft MVP - Visual Basic
-
I'm not sure if you can do anything but brute force it. Here's one way: // Read the file into a byte array FileStream fs = File.Open("TextFile1.txt", FileMode.Open); byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, (int)fs.Length); // int to store num of lines int numOfLines = 0; // if the file is not empty, we have at least 1 line if (bytes.Length > 0) numOfLines++; // increment the count for each newline character found foreach (byte b in bytes) if(b == '\n') numOfLines ++; ... This is just a starting point and would need to be more robust depending on the requirements. If the textfile's length is greater than int.MaxValue I suppose you'd need to read the file in sections. This solution will count all lines (even blank lines and trailing blank lines). I would be interested to see another solution. I'm sure there must be a better one. Cheers, Ian
I would personally prefer to use StreamReader, it certainly looks more elegant.
int lineCount = 0; // Create an instance of StreamReader to read from a file. // The using statement also closes the StreamReader. using (System.IO.StreamReader sr = new System.IO.StreamReader("myFile.txt")) { // Read and display lines from the file until the end of the file is reached. while ( sr.ReadLine() != null) lineCount++; } Console.WriteLine( "Line Count: " + lineCount.ToString() );
Obviously you should add some error checking like making sure the file exists ETC but that should otherwise do the job. Hope it helps
Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students View my Blog
-
I would personally prefer to use StreamReader, it certainly looks more elegant.
int lineCount = 0; // Create an instance of StreamReader to read from a file. // The using statement also closes the StreamReader. using (System.IO.StreamReader sr = new System.IO.StreamReader("myFile.txt")) { // Read and display lines from the file until the end of the file is reached. while ( sr.ReadLine() != null) lineCount++; } Console.WriteLine( "Line Count: " + lineCount.ToString() );
Obviously you should add some error checking like making sure the file exists ETC but that should otherwise do the job. Hope it helps
Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students View my Blog
Thanks. I knew there had to be a better way.
-
How to get the count of lines in .txt file. Help.
C#
What counts as a "line"? A certain non-printing character? (Then just count the number of that character.) A certain line length? (Then take (total chars)/(line length)). Etc.