How to know the number of lines a large file has in C#?

Asked

Viewed 4,066 times

5

Well, I’m starting now in C#, but I already have a great experience with PHP and I know more or less Python.

And the means that I use to appeal is to do something that I found difficult to do in a language that I know in a language that I know nothing about.

I asked here how to count the lines of a large file in PHP, because it is something that is not so simple to do in the language, does not have a specific function for it, I have to do maneuvers.

Now that I’m learning C#, i wonder how do I know how many lines has a large file.

But I would like each function used to be explained, because I don’t know much about C# yet.

I want to read a JSON of 6.6MB here from my machine and know how many lines it has through the C#.

How can I do that?

2 answers

6


The class File (System.IO.File), has a method ReadLines.

It makes a lazy load of the file lines, that is, a certain line will only be loaded into memory when prompted.

So you can do:

var qtdLinhas = File.ReadLines("C:\\Users\\Wallace\\arquivo.json").Count();
  • 2

    Well, that’s better that PHP then +1

  • I just think it’s important to remember that Count forces the reading of the entire file, which is not very clear in the code, but basically the only way to know how many lines a file has is to go reading and counting how many lines there are in it.

  • @Leandrogodoyrosa since this reading does not load all the lines into the memory, no problem. The goal really is to read a large file, I believe the answer meets

  • 1

    If your goal is to read the entire file, doing this "before" count is silly because you will end up reading the file twice. What is the purpose of knowing the number of lines? Would it be to use in a progress bar (that is, estimating the completed percentage)? If it is, I would suggest "estimating" the number of lines based on the file size (it averages 80 characters per line, for example). Or, more precisely, use the number of characters read. :)

  • Actually the question does not apply to a specific context @Luizvieira. It was a question from AP.

  • Sorry, I ended up posting in your reply unintentionally. But my comment is precisely for AP. :)

  • 1

    I had noticed :P

Show 2 more comments

5

More manually, you could also use this algorithm (also working with System.IO)...

In Stream Peek returns the next character, without advancing the Stream pointer... and if there are no more characters it returns -1, so I took advantage of it to use in while... at least in my tests (I have no file of 6mb) my algorithm took half a thousandth to read a 103kb text file... as File.Readline took 22 thousandths... inserir a descrição da imagem aqui

well, both algorithms work well, with great performance, it’s up to you to choose...

string filename = "c:\input.txt"
TextReader Leitor = new StreamReader(filename, true);//Inicializa o Leitor
int Linhas = 0;
while (Leitor.Peek() != -1) {//Enquanto o arquivo não acabar, o Peek não retorna -1 sendo adequando para o loop while...
    Linhas++;//Incrementa 1 na contagem
    Leitor.ReadLine();//Avança uma linha no arquivo
}
Leitor.Close(); //Fecha o Leitor, dando acesso ao arquivo para outros programas....
Console.WriteLine("Este Arquivo contém " + Linhas + " Linhas.");
Console.ReadKey();
  • Really time is quite matched +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.