0

I have a file text which splited by newline character. the text file such this:

merah.dc
kuning.dc
hijau.dc
biru.dc
orange.dc
mahopermanent.dc

I want to count them by splitting with a newline character. so, the count of them are 6. I could only do this with looping:

int count = 0;
string path = "directory\\admin.txt";
StreamReader moco = File.OpenText(path);
string s; 
while ((s = moco.ReadLine())!= null)
{
    count++;
}

I want to count them with a simple way like the PHP syntax:

<?php
$file = file("directory\\admin.txt");
$count = count($file);
echo $count;
?>

The above syntax able to counts them without looping. Just use file() and count(). is any function in C# which equals with that function ?

Ondrej Janacek
  • 12,236
  • 14
  • 55
  • 90
RieqyNS13
  • 444
  • 1
  • 5
  • 12
  • See [this](http://stackoverflow.com/a/119572/2974754) answer. – Yurii Dec 22 '13 at 17:49
  • possible duplicate of [Determine the number of lines within a text file](http://stackoverflow.com/questions/119559/determine-the-number-of-lines-within-a-text-file) – Rohit Vats Dec 22 '13 at 17:50

5 Answers5

3

You can use ReadLines without loading whole file into the memory (of course this method can be useful if your file is large)

int count = File.ReadLines(filename).Count();
L.B
  • 110,417
  • 19
  • 170
  • 215
  • 2
    +1 I continue to forget that method. Started using ReadAllLines and still not getting into the IEnumerable tricks. However should be mentioned that this is available from NET 4.0 onward – Steve Dec 22 '13 at 17:55
0

Step 1 : You can use ReadAllLines() function to get all Lines from the given file path as a String Array.

Step 2: you can invoke Length property on the obtained String Array to get the Count of Total Lines

Try This:

using System.IO;

String [] allLines=File.ReadAllLines(@"directory\admin.txt");
int length=allLines.Length;
Sudhakar Tillapudi
  • 25,307
  • 5
  • 35
  • 65
0
int count = File.ReadAllLines(@"directory\\admin.txt").Length;
Casimir et Hippolyte
  • 85,718
  • 5
  • 90
  • 121
Kurubaran
  • 8,342
  • 5
  • 37
  • 63
0

You don't need to split them by a new character, use File.ReadAllLines (which returns an array of strins, each element is a line).

int lineCount = File.ReadAllLines("test.txt").Length;

If you want to use split, then:

int lineCount = File.ReadAllText("test.txt").Split('\n').Count();

ReadAllText returns a strin with the entire content of the file.

Sudhakar Tillapudi
  • 25,307
  • 5
  • 35
  • 65
Rodrigo Silva
  • 432
  • 1
  • 6
  • 18
  • ReadAllLines it about 200/300 ticks faster than the latter (time measured with a stopwatch). – Rodrigo Silva Dec 22 '13 at 17:59
  • @RodrigoSilva - Any reference for your statement - `ReadAllLines it about 200/300 ticks faster than the latter`? – Rohit Vats Dec 22 '13 at 18:02
  • You're right, it was an "unfounded" statement. I'll correct it: my measurements concluded it took a bit less time. I can't explain the theoretical reason behind it nor do I claim my results are 100% accurate, they're just rough tests. – Rodrigo Silva Dec 22 '13 at 18:04
0

You can Split() the textfile

StreamReader moco = File.OpenText(path);
string s;
string splitArray[] = s.Split('\n'); //Split on newline

int length = splitArray[].Length;