Connecting Tech Pros Worldwide Forums | Help | Site Map

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

deko
Guest
 
Posts: n/a
#1: Jul 17 '05
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!



Garp
Guest
 
Posts: n/a
#2: Jul 17 '05

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



"deko" <dje422@hotmail.com> wrote in message
news:dpN7c.41351$Hs5.29020@newssvr25.news.prodigy. com...[color=blue]
> I'm not sure how to do this in php - need to calculate and delete an[/color]
unspecified[color=blue]
> 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![/color]

$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


deko
Guest
 
Posts: n/a
#3: Jul 17 '05

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


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" <garp7@no7.blueyonder.co.uk> wrote in message
news:hQQ7c.966$UP5.8030281@news-text.cableinet.net...[color=blue]
>
> "deko" <dje422@hotmail.com> wrote in message
> news:dpN7c.41351$Hs5.29020@newssvr25.news.prodigy. com...[color=green]
> > I'm not sure how to do this in php - need to calculate and delete an[/color]
> unspecified[color=green]
> > 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![/color]
>
> $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
>[/color]



deko
Guest
 
Posts: n/a
#4: Jul 17 '05

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


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);


Garp
Guest
 
Posts: n/a
#5: Jul 17 '05

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


> the problem with reading the file into an array is that the lines are
irregular:[color=blue]
>
> 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[/color]
fread[color=blue]
> to copy what I want into a variable, then just paste it back in - but the[/color]
syntax[color=blue]
> for this is killing me...
>
> developing...
>
>
> "Garp" <garp7@no7.blueyonder.co.uk> wrote in message
> news:hQQ7c.966$UP5.8030281@news-text.cableinet.net...[color=green]
> >
> > "deko" <dje422@hotmail.com> wrote in message
> > news:dpN7c.41351$Hs5.29020@newssvr25.news.prodigy. com...[color=darkred]
> > > I'm not sure how to do this in php - need to calculate and delete an[/color]
> > unspecified[color=darkred]
> > > 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![/color]
> >
> > $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[/color][/color]

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


Closed Thread