Jump to content


Photo

Read file in reverse order


  • Please log in to reply
5 replies to this topic

#1 Zett

Zett
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 13 April 2006 - 09:17 PM

Hi. I'm wondering if any of you could help me with the algorithm/code for reading a text file line-by-line in a reverse order. The delimiter is a newline character "\n".

Basically I just want to put the pointer to the end of the file and then read it in a reverse (bottom-up) order.

Thanks in advance!

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 April 2006 - 09:29 PM

Here's one way of doing it:
  • Use the file() function to read the file into an array.
  • Use the array_reverse() function to reverse the array.
  • Use a foreach loop to process each line.
Example:
<?php
$input = file('input_file.txt');
$rev_input = array_reverse($input);
foreach ($rev_input as $line)
      echo trim($line) . '<br>';
?>

Ken

#3 Zett

Zett
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 13 April 2006 - 09:49 PM

I've tried that and it works well. However, the reason I'm looking for something else is that file() will not be a good option when dealing with large files.

I'm working on a guestbook where latest entries are stored at the end of the file. And, of course, latest entries are displayed first.
Let's say I have 100 or 200 entries stored in the file, and I only need the last 10. It would be a waste of time and space to copy the whole file into an array and then choose 10 entries. What if there were 1000 or 10 000 entries in the file (I hope you get the idea).

I can also simply scan throw the whole file without copying anything until I come to the 10th last entry. From that point to the end I get my 10 latest entries.

It would be great if there was a function like fgetrs (like strpos/strrpos) for reading file backwards.

#4 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 13 April 2006 - 11:01 PM

you could write the file in reverse order... that or use a mysql database and the whole problem ceases to exist.

#5 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 13 April 2006 - 11:19 PM

Hmmmm, ok here comes hammer-man again.

1. each entry is placed in its own file. the name of the file is the timestamp of when the file is created.

2. place the timestamp/filename into an "index" file

3. when accessing simply place the index file into an array and sort the array(even with 100's of entries still rather quick)

4. use an array function to "grab' the 10/20/30/ target timestamps and display them

???

wadda ya think?

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#6 Zett

Zett
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 14 April 2006 - 02:17 AM

That idea would work for a news system or something similar to that. The problem is that it's a guestbook, sometimes people post a lot of messages like "Nice site", "Hi" and so on. So, a file for each post would be too much.

Ok, I found a solution. Actually, it's not a solution, it's a workaround. I'm going to use txtSQL which is a pretty neat thing. It's somewhat a flat-file version of mysql.

But the problem of reading the file in reverse order is present and I will trying to find a way to do it. I really want to find the answer because this question arose a while ago but I never really had time to look into this problem.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users