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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deko

    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".$visit or;)
    //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

    #2
    Re: How to delete N lines from the top of a file?


    "deko" <dje422@hotmail .com> wrote in message
    news:dpN7c.4135 1$Hs5.29020@new ssvr25.news.pro digy.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".$visit or;)
    > //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($filea rray);
    for($i=($c < 10 ? 0 : $c-10);$i < $c;++$i) {
    fputs($fp,$file array[$i]);
    }
    fclose($fp);

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

    Garp


    Comment

    • deko

      #3
      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.blue yonder.co.uk> wrote in message
      news:hQQ7c.966$ UP5.8030281@new s-text.cableinet. net...[color=blue]
      >
      > "deko" <dje422@hotmail .com> wrote in message
      > news:dpN7c.4135 1$Hs5.29020@new ssvr25.news.pro digy.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".$visit or;)
      > > //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($filea rray);
      > for($i=($c < 10 ? 0 : $c-10);$i < $c;++$i) {
      > fputs($fp,$file array[$i]);
      > }
      > fclose($fp);
      >
      > (Handcrafted and untested at 6am, no warranty implied).
      >
      > Garp
      >[/color]



      Comment

      • deko

        #4
        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($visda ta));
        $fp = fopen($visdata, "w");
        fwrite($fp, $visitors);
        fclose($fp);


        Comment

        • Garp

          #5
          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.blue yonder.co.uk> wrote in message
          > news:hQQ7c.966$ UP5.8030281@new s-text.cableinet. net...[color=green]
          > >
          > > "deko" <dje422@hotmail .com> wrote in message
          > > news:dpN7c.4135 1$Hs5.29020@new ssvr25.news.pro digy.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".$visit or;)
          > > > //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($filea rray);
          > > for($i=($c < 10 ? 0 : $c-10);$i < $c;++$i) {
          > > fputs($fp,$file array[$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


          Comment

          Working...