470,849 Members | 1,141 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

How to delete N lines from the top of a file?

I'm not sure how to do this in php - need to calculate and delete an unspecified
number of lines from the *top* of a file.

<?php
//append $visitor to the bottom of $visdata
$fp = fopen($visdata,"a");
fwrite($fp, "\n".$visitor;)
//count the lines in $visdata
$lines = count($visdata);
if $lines > 10
{
//delete as many lines off the top as necessary
//to end up with 10 lines total in $visdata
}
fclose($fp);
?>

suggestions welcome! Thanks!
Jul 17 '05 #1
4 2503

"deko" <dj****@hotmail.com> wrote in message
news:dp*******************@newssvr25.news.prodigy. com...
I'm not sure how to do this in php - need to calculate and delete an unspecified number of lines from the *top* of a file.

<?php
//append $visitor to the bottom of $visdata
$fp = fopen($visdata,"a");
fwrite($fp, "\n".$visitor;)
//count the lines in $visdata
$lines = count($visdata);
if $lines > 10
{
//delete as many lines off the top as necessary
//to end up with 10 lines total in $visdata
}
fclose($fp);
?>

suggestions welcome! Thanks!


$filearray=file($visdata);
$fp = fopen($visdata,"w");
$c=count($filearray);
for($i=($c < 10 ? 0 : $c-10);$i < $c;++$i) {
fputs($fp,$filearray[$i]);
}
fclose($fp);

(Handcrafted and untested at 6am, no warranty implied).

Garp
Jul 17 '05 #2
the problem with reading the file into an array is that the lines are irregular:

abd245cf | dsgf ; . | d.kjfd | ::kdijh | . | kjdf | | ""

I can't modify what that string looks like, so I'm not sure if I can use
explode.

perhaps I could append the new string on the bottom, then use fseek and fread
to copy what I want into a variable, then just paste it back in - but the syntax
for this is killing me...

developing...
"Garp" <ga***@no7.blueyonder.co.uk> wrote in message
news:hQ*******************@news-text.cableinet.net...

"deko" <dj****@hotmail.com> wrote in message
news:dp*******************@newssvr25.news.prodigy. com...
I'm not sure how to do this in php - need to calculate and delete an

unspecified
number of lines from the *top* of a file.

<?php
//append $visitor to the bottom of $visdata
$fp = fopen($visdata,"a");
fwrite($fp, "\n".$visitor;)
//count the lines in $visdata
$lines = count($visdata);
if $lines > 10
{
//delete as many lines off the top as necessary
//to end up with 10 lines total in $visdata
}
fclose($fp);
?>

suggestions welcome! Thanks!


$filearray=file($visdata);
$fp = fopen($visdata,"w");
$c=count($filearray);
for($i=($c < 10 ? 0 : $c-10);$i < $c;++$i) {
fputs($fp,$filearray[$i]);
}
fclose($fp);

(Handcrafted and untested at 6am, no warranty implied).

Garp


Jul 17 '05 #3
I couldn't find a way to delete a specific number of "lines" from the top of the
file in an effort to keep the file at 10 lines. But this seems work just as
well by limiting the size of the file - although it's not as precise since I
can't know how long each line will be; $new_visitor is a bunch of data about
visitors that can vary widely.

$fp = fopen($visdata,"a");
fwrite($fp, $new_visitor."\n");
$fp = fopen($visdata,"r");
fseek($fp, -10240, SEEK_END);
$visitors = fread($fp, filesize($visdata));
$fp = fopen($visdata,"w");
fwrite($fp, $visitors);
fclose($fp);
Jul 17 '05 #4
> the problem with reading the file into an array is that the lines are
irregular:

abd245cf | dsgf ; . | d.kjfd | ::kdijh | . | kjdf | | ""

I can't modify what that string looks like, so I'm not sure if I can use
explode.

perhaps I could append the new string on the bottom, then use fseek and fread to copy what I want into a variable, then just paste it back in - but the syntax for this is killing me...

developing...
"Garp" <ga***@no7.blueyonder.co.uk> wrote in message
news:hQ*******************@news-text.cableinet.net...

"deko" <dj****@hotmail.com> wrote in message
news:dp*******************@newssvr25.news.prodigy. com...
I'm not sure how to do this in php - need to calculate and delete an

unspecified
number of lines from the *top* of a file.

<?php
//append $visitor to the bottom of $visdata
$fp = fopen($visdata,"a");
fwrite($fp, "\n".$visitor;)
//count the lines in $visdata
$lines = count($visdata);
if $lines > 10
{
//delete as many lines off the top as necessary
//to end up with 10 lines total in $visdata
}
fclose($fp);
?>

suggestions welcome! Thanks!


$filearray=file($visdata);
$fp = fopen($visdata,"w");
$c=count($filearray);
for($i=($c < 10 ? 0 : $c-10);$i < $c;++$i) {
fputs($fp,$filearray[$i]);
}
fclose($fp);

(Handcrafted and untested at 6am, no warranty implied).

Garp


I'm going to say three more things, then I'm going to start charging, or
posting my own bugs. 8)

1) Stop top-posting, or I'm-a have to kill you.
2) You asked for a way of cutting all but the bottom ten lines. In my world,
a "line" terminates in a linefeed. file() reads a file in, dividing by
linefeeds and returning an array, and with the minimum of fuss.
3) With the amount of work you've put in to this, you could have learned the
MySQL interfaces.

Still, I wish you luck.

Garp
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Srinivasa T.N. | last post: by
4 posts views Thread by Ignoramus6539 | last post: by
24 posts views Thread by biganthony via AccessMonster.com | last post: by
3 posts views Thread by Barkingmadscot | last post: by
2 posts views Thread by Francesco Pietra | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.