By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,719 Members | 1,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,719 IT Pros & Developers. It's quick & easy.

a fast line counter

P: n/a
jf
Hi,

I've a big file, and I want to count the number of lines.

I tried this:
$i=0;
$fp =fopen('file','r');
while ( ! feof ($fp) ) {
$ligne = fgets($fp,8096);
$i++;
}

and this:
$i = exec("wc -l file");

The first solution is slower, but the second isn't cross system.

How can I do fast and clean ?

Thanks.
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
.oO(jf)
I've a big file, and I want to count the number of lines.

I tried this:
$i=0;
$fp =fopen('file','r');
while ( ! feof ($fp) ) {
$ligne = fgets($fp,8096);
$i++;
}

and this:
$i = exec("wc -l file");
There's a third:

$i = count(file('file')); // without error checking
The first solution is slower, but the second isn't cross system.

How can I do fast and clean ?


Put it all into a function, that first tries to determine the running OS
(check the predefined constant PHP_OS). If you're on Win there's not
much of a choice - go the safe way.

If you're on Linux or something else that might have the wc command, try
to execute it, but watch the returned error code of exec(). If the
command doesn't exist or the execution fails use the safe method.

Micha
Jul 17 '05 #2

P: n/a

<?php

function get_numlines($file)
{
**$lines = count(file($file));
return $lines
}

$myfile = "/path/to/your/dir/somefile.txt";
echo get_numlines($myfile);
?>

On 9/24/04 6:26 PM, in article 20*********************@wxp.af, "jf"
<no@no.spam> wrote:
Hi,

I've a big file, and I want to count the number of lines.

I tried this:
$i=0;
$fp =fopen('file','r');
while ( ! feof ($fp) ) {
$ligne = fgets($fp,8096);
$i++;
}

and this:
$i = exec("wc -l file");

The first solution is slower, but the second isn't cross system.

How can I do fast and clean ?

Thanks.


Jul 17 '05 #3

P: n/a
jf
Bzzzzzzz...
Wrong answer !
:)

It's a big file, with file($file) php load all the file in memory: "Allowedmemory size exhausted". I don't want to extend the allowed memory.

Regards.
JF

On Fri, 24 Sep 2004 22:57:44 GMT
Jamie Davison <jd**********@yahoo.com> wrote:



<?php

function get_numlines($file)
{
**$lines = count(file($file));
return $lines
}

$myfile = "/path/to/your/dir/somefile.txt";
echo get_numlines($myfile);
?>





On 9/24/04 6:26 PM, in article 20*********************@wxp.af, "jf"
<no@no.spam> wrote:
Hi,

I've a big file, and I want to count the number of lines.

I tried this:
$i=0;
$fp =fopen('file','r');
while ( ! feof ($fp) ) {
$ligne = fgets($fp,8096);
$i++;
}

and this:
$i = exec("wc -l file");

The first solution is slower, but the second isn't cross system.

How can I do fast and clean ?

Thanks.


Jul 17 '05 #4

P: n/a
"jf" <no@no.spam> wrote in message news:20*********************@wxp.af...
Hi,

I've a big file, and I want to count the number of lines.

I tried this:
$i=0;
$fp =fopen('file','r');
while ( ! feof ($fp) ) {
$ligne = fgets($fp,8096);
$i++;
}


Instead of fgets(), grab the content in larger chunks with fread(), then
count the occurrence of \n with substr_count().

$i=0;
$fp =fopen('file','r');
while ($chunk = fread($p, 1024000)) {
$i += substr_count($chunk, "\n");
}

Jul 17 '05 #5

P: n/a
Chung Leong <ch***********@hotmail.com> wrote:
"jf" <no@no.spam> wrote in message news:20*********************@wxp.af...
Hi,

I've a big file, and I want to count the number of lines.

I tried this:
$i=0;
$fp =fopen('file','r');
while ( ! feof ($fp) ) {
$ligne = fgets($fp,8096);
$i++;
}


Instead of fgets(), grab the content in larger chunks with fread(), then
count the occurrence of \n with substr_count().

$i=0;
$fp =fopen('file','r');
while ($chunk = fread($p, 1024000)) {
$i += substr_count($chunk, "\n");
}


Bad luck if it's a macintosh file :o)
Maybe he should count \r in a different variable, too, and take the maximum
(of \n and \r) afterwards.
--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.