Efficiently Counting the Number of Lines of a Text File. (200Mb+)

Efficiently counting the number of lines of a text file. (200mb+)

This will use less memory, since it doesn't load the whole file into memory:

$file="largefile.txt";
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
}

fclose($handle);

echo $linecount;

fgets loads a single line into memory (if the second argument $length is omitted it will keep reading from the stream until it reaches the end of the line, which is what we want). This is still unlikely to be as quick as using something other than PHP, if you care about wall time as well as memory usage.

The only danger with this is if any lines are particularly long (what if you encounter a 2GB file without line breaks?). In which case you're better off doing slurping it in in chunks, and counting end-of-line characters:

$file="largefile.txt";
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle, 4096);
$linecount = $linecount + substr_count($line, PHP_EOL);
}

fclose($handle);

echo $linecount;

How to get Number Of Lines without Reading File To End

No. You have to read the file. Consider storing it at the beginning of the file or in a separate file when you write the file if you want to find it quickly without counting.

Note that you can stream the file, and it's surprisingly fast:

int count = File.ReadLines(path).Count();

Because i might be in some cases where i should get Total Number of Line's and compare it to Current line to display the Percentage,and just for a Percentage Display it might be stupid to read first all Content than read it Again to Display the raw text at user.

Oh, just get the file size and the length of each line in bytes and keep a cumulative count of the number of bytes processed so far.

Count lines of file in PHP on Windows

I prefer to just loop through the file, reading a line each time and incrementing a counter, using and counting the array returned by file() is only good for smaller files.

<?php

$loc = 'Ubuntu - 10.10 i386.iso';

$f = fopen($loc,'r');
$count = 0;

while (fgets($f)) $count++;

fclose($f);

print "Our file has $count lines" . PHP_EOL;

if you'd use file() for a such a large file it would read it completely into memory, which can be prohibitive depending on your situation. If this is a 1-time "I don't care, it's my workstation and I have enough memory" situation or the files are guaranteed to be small then you could use

count(file($loc));

Otherwise I'd loop through, especially since if action would have to be performed by many processes. Both ways of counting loop through the whole of the file but memory increases vastly in the second case.

How many lines in a file with count always returns 1

You can try this :

$lines = 0;

if ($fh = fopen('file.txt', 'r')) {
while (!feof($fh)) {
if (fgets($fh)) {
$lines++;
}
}
}
echo $lines; // line count

PHP: clear a text file if it exceeds 50 lines

$file = 'idata.txt';
$lines = count(file($file));
if ($lines > 50){
$fh = fopen( 'idata.txt', 'w' );
fclose($fh);
}


Related Topics



Leave a reply



Submit