Count the number of lines in a text file

From CodeCodex

Implementations[edit]

Erlang[edit]

count_line(Filename) ->
    case file:open(Filename, [read]) of
        {ok, IoDevice} ->
            Count = count_line(IoDevice, 0),
            file:close(IoDevice),
            Count;
        {error, Reason} ->
            io:format("~s open error  reason:~s~n", [Filename, Reason]),
            ng
    end.

count_line(IoDevice, Count) ->
    case file:read_line(IoDevice) of
        {ok, _} -> count_line(IoDevice, Count+1);
        eof     -> Count
    end.

Java[edit]

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;

public class ReadFile {
    public static int countLines(final String filename) {
        StreamTokenizer st = null;
        Reader reader = null;
        try {
            reader = new BufferedReader(new FileReader(filename));
            st = new StreamTokenizer(reader);
            while (st.nextToken() != StreamTokenizer.TT_EOF) {
            }
        } catch (final IOException ioe) {
            System.err.println("IOException occurred:");
            ioe.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (final IOException ignored) {
                // deliberately ignored
            }
        }
        return st.lineno();
    }

    public static void main(final String arrrggghh[]) {
        final int lines = ReadFile.countLines("/Cucu_Video_log.txt");
        System.out.println("file has " + lines + " lines in it");
    }
}

Perl[edit]

my $line_count = 0;

open my $fh, '<', $filename or die qq{Cannot open "$filename": $!};

while ( sysread $fh, my $buffer, 4096 ) {
    $line_count += ($buffer =~ tr/\n//);
}

close $fh;

Original Source: Perl documentation (perlfaq5)


PHP[edit]

Extremely simple PHP sample to count and return the number of lines in a text file. The output can be customized to suit your own requirements.

<?php
  $file = "somefile.txt";
  $lines = count(file($file));
  echo "There are $lines lines in $file";
?>

Python[edit]

filename = "somefile.txt"
myfile = open(filename)
lines = len(myfile.readlines())
print "There are %d lines in %s" % (lines, filename)

Rexx[edit]

version 1:

/* Count the number of lines in a file
   and store them in a stem
*/
Parse Arg File
i=1
time1=Time("E")
time1=Time("R")
Do While LINES(file)
   Data.i=LINEIN(file)
   i=i+1
End
time2=Time("E")
ET=Time2-Time1
Say "It took" ET "seconds to read the file."
Say "There were" i-1 "lines in" file
Exit

version 2:

/*count the number of lines in a file and store them in a stem*/
parse arg fileID
call time 'R'                    /*reset the wall clock timer.*/

    do j=1 while lines(fileID)   /*keep reading until E-O-F). */
    data.j=linein(fileID)
    end 

say 'There are' j-1 "lines in the file:" fileID
say 'and it took' format(time('E'),,2) "seconds to read."

Ruby[edit]

filename = "somefile.txt"
count = IO.readlines(filename).size
puts "There are #{count} lines in #{filename}";