How do i read a file line by line backwards?

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

    How do i read a file line by line backwards?

    Can anybody help.

    I need to read a txt file backwords line by line. Can anybody help me do
    this.

    Thanks
    Chris
  • Daniel Tryba

    #2
    Re: How do i read a file line by line backwards?

    Chris <cmw@raindrop.c o.uk> wrote:[color=blue]
    > I need to read a txt file backwords line by line. Can anybody help me do
    > this.[/color]

    Use file(). It returns an array which you can read from count($array)-1
    to 0.

    --

    Daniel Tryba

    Comment

    • Ian.H

      #3
      Re: How do i read a file line by line backwards?

      On Mon, 05 Jan 2004 15:17:47 -0800, Chris wrote:
      [color=blue]
      > Can anybody help.
      >
      > I need to read a txt file backwords line by line. Can anybody help me do
      > this.
      >
      > Thanks
      > Chris[/color]


      $foo = @file('foo.php' );
      $bar = array_reverse($ foo);

      foreach ($foo as $baz) {
      print $baz . "\n";
      }


      HTH =)



      Regards,

      Ian

      --
      Ian.H [Design & Development]
      digiServ Network - Web solutions
      www.digiserv.net | irc.digiserv.ne t | forum.digiserv. net
      Programming, Web design, development & hosting.

      Comment

      • Rahul Anand

        #4
        Re: How do i read a file line by line backwards?

        You can also use function *array_reverse* to reverse the array, after
        calling *file*. But dont do this unnecessarily as it will put extra
        load on the server.

        You can even reverse a string using following code:

        $str = implode("",arra y_reverse(preg_ split('//', $str)));

        --
        Rahul

        Daniel Tryba <news_comp.lang .php@canopus.nl > wrote in message news:<btcrgc$c1 m$3@news.tue.nl >...[color=blue]
        > Chris <cmw@raindrop.c o.uk> wrote:[color=green]
        > > I need to read a txt file backwords line by line. Can anybody help me do
        > > this.[/color]
        >
        > Use file(). It returns an array which you can read from count($array)-1
        > to 0.[/color]

        Comment

        • jn

          #5
          Re: How do i read a file line by line backwards?


          "Ian.H" <ian@WINDOZEdig iserv.net> wrote in message
          news:pan.2004.0 1.06.00.04.24.5 3974@hybris.dig iserv.net...[color=blue]
          > On Mon, 05 Jan 2004 15:17:47 -0800, Chris wrote:
          >[color=green]
          > > Can anybody help.
          > >
          > > I need to read a txt file backwords line by line. Can anybody help me do
          > > this.
          > >
          > > Thanks
          > > Chris[/color]
          >
          >
          > $foo = @file('foo.php' );
          > $bar = array_reverse($ foo);
          >
          > foreach ($foo as $baz) {
          > print $baz . "\n";
          > }
          >
          >
          > HTH =)
          >
          >
          >
          > Regards,
          >
          > Ian
          >
          > --
          > Ian.H [Design & Development]
          > digiServ Network - Web solutions
          > www.digiserv.net | irc.digiserv.ne t | forum.digiserv. net
          > Programming, Web design, development & hosting.
          >
          >[/color]

          That should be:

          foreach ($bar as $baz) {
          print $baz . "\n";
          }



          Comment

          • Chris

            #6
            Re: How do i read a file line by line backwards?

            Daniel Tryba <news_comp.lang .php@canopus.nl > wrote in message news:<btcrgc$c1 m$3@news.tue.nl >...[color=blue]
            > Chris <cmw@raindrop.c o.uk> wrote:[color=green]
            > > I need to read a txt file backwords line by line. Can anybody help me do
            > > this.[/color]
            >
            > Use file(). It returns an array which you can read from count($array)-1
            > to 0.[/color]

            Is their a way of doing this without reading the whole file into
            memory as it could potentially be a large file?

            thanks
            chris

            Comment

            • Shawn Wilson

              #7
              Re: How do i read a file line by line backwards?

              Chris wrote:[color=blue]
              >
              > Daniel Tryba <news_comp.lang .php@canopus.nl > wrote in message news:<btcrgc$c1 m$3@news.tue.nl >...[color=green]
              > > Chris <cmw@raindrop.c o.uk> wrote:[color=darkred]
              > > > I need to read a txt file backwords line by line. Can anybody help me do
              > > > this.[/color]
              > >
              > > Use file(). It returns an array which you can read from count($array)-1
              > > to 0.[/color]
              >
              > Is their a way of doing this without reading the whole file into
              > memory as it could potentially be a large file?[/color]

              In this case, I would recommend something like the following (ghastly) mix of
              code and pseudocode :o). I haven't tested it, so there may be a logic error -
              don't treat is as gospel. Basically, you open the file and read it chunk by
              chunk backwards (that is, the chunks start at the end and move towards the
              beginning of the file. Each individual chunk is read forwards). Make each
              chunk into an array. Unless you're reading from the beginning of the file,
              remember how big the first record is and then kill it, as it's likely
              incomplete. During the next loop, adjust your fseek to compensate for each
              previous killed record. Then read through the array backwards and do whatever
              it is you want to the records. Remember, it's good practice not to loop through
              the whole thing if you don't need to. In other words, use "break" and
              "continue" as appropriate.

              fopen(); //open file
              $intChunkSize = 5 * 1024; //set chunk size to bigger than max record size
              $intAdjustment = 0;
              $intSeekTo = 1;

              loop //loop while $intSeekTo > 0
              $intSeekTo = filesize() - ($intChunkSize * $intLoopNumber) + $intAdjustment;
              if ($intSeekTo < 0){
              $intChunkSize += intSeekTo; //adjust your chunk size to bytes remaining
              $intSeekTo = 0;
              }

              fseek(); //fseek to $intSeekTo
              fread(); //read $intChunkSize bytes from file

              $arrFoo = split("\n", fread()); //read up to intChunksize bytes into array
              $intAdjustment += strlen($arrFoo[0]); //remember how much to adjust
              if ($intSeekTo > 0) //if this isn't the last loop
              array_shift($ar rFoo); //kill the first (incomplete) record
              for ($i = count($arrFoo)-1; $i >= 0; ++$i) //loop through array backwards
              //do your thing

              end loop

              Regards,
              Shawn
              --
              Shawn Wilson
              shawn@glassgian t.com

              Comment

              • CountScubula

                #8
                Re: How do i read a file line by line backwards?

                "Chris" <cmw@raindrop.c o.uk> wrote in message
                news:bb9cc180.0 401051517.21ebb de4@posting.goo gle.com...[color=blue]
                > Can anybody help.
                >
                > I need to read a txt file backwords line by line. Can anybody help me do
                > this.
                >
                > Thanks
                > Chris[/color]
                This is just a thought,

                why not just open the file, seek to the end, read byte by byte backwords,
                untill you find a \n

                then what you have read in is a line,

                repeat for next line,

                repeat for next line,

                repeat for next line,

                --
                Mike Bradley
                http://www.gzentools.com -- free online php tools


                Comment

                • Kevin Thorpe

                  #9
                  Re: How do i read a file line by line backwards?

                  Chris wrote:[color=blue]
                  > Can anybody help.
                  >
                  > I need to read a txt file backwords line by line. Can anybody help me do
                  > this.[/color]

                  Are you on Linux? If so:

                  $file = popen("tac $filename",'r') ;
                  while ($line = fgets($file,809 6) {
                  echo $line;
                  }

                  tac == cat backwards (lame joke but there you go)

                  Comment

                  Working...