How to count lines fast?

前端 未结 6 1414
感情败类
感情败类 2020-12-09 15:34

I tried unxutils\' wc -l but it crashed for 1GB files. I tried this C# code

long count = 0;
using (StreamReader r = new StreamReader(f))
{
    st         


        
6条回答
  •  长情又很酷
    2020-12-09 15:55

    Have you tried flex?

    %{
    long num_lines = 0;
    %}
    %option 8bit outfile="scanner.c"
    %option nounput nomain noyywrap
    %option warn
    
    %%
    .+ { }
    \n { ++num_lines; }
    %%
    int main(int argc, char **argv);
    
    int main (argc,argv)
    int argc;
    char **argv;
    {
    yylex();
    printf( "# of lines = %d\n", num_lines );
    return 0;
    }
    

    Just compile with:

    flex -Cf scanner.l 
    gcc -O -o lineCount.exe scanner.c
    

    It accepts input on stdin and outputs the number of lines.

提交回复
热议问题