473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

a fast line counter

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
5 15108
.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

<?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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tan Thuan Seah | last post by:
Hi all, I am currently coding a sparse factorization program for my project. I intend to make a comparison of the column based method and multifrontal method in terms of running time, but I am...
0
by: Earl Anderson | last post by:
KB Article Q140908 provided the following function to create an Auto Incrementing Counter: Function Next_Custom_Counter () On Error GoTo Next_Custom_Counter_Err Dim MyDB As Database Dim...
2
by: Paul Brown | last post by:
Thanks for various responses - my feeling is that the cost benefit ratio is now approaching the noise level asymptote, though there was at least one good point : > compressed plain-text copy of...
7
by: Rafael | last post by:
Hello Everyone, I need some major help with this code I have for a file viewer. I need it to stop at the 24th line. What Syntax do I need for this to happen? Here is the code.... #include...
14
by: WStoreyII | last post by:
the following code is supposed to read a whole line upto a new line char from a file. however it does not work. it is producing weird results. please help. I had error checking in there for...
11
by: jo3c | last post by:
hi everybody im a newbie in python i need to read line 4 from a header file using linecache will crash my computer due to memory loading, because i am working on 2000 files each is 8mb ...
7
by: APEJMAN | last post by:
Hi First of all I should say, I dont want to bother any one with my question or long program please if you have time, help me.( I dont mean that I want any one to do my program, just please if you...
27
by: Mark | last post by:
Hi all, I have a scenario where I have a list like this: User Score 1 0 1 1 1 5 2 3 2 1
20
by: lightning18 | last post by:
I have a txt file with words in it and I had to print the incorrect words into a set which I have. Now I need to find in which line the incorrect words are in the text file and print it as a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.