473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

reverse reading a text file

xEM
how can i read for example 10 last lines from a text file beginning
from last line? do you have some idea?

in a different manner... how can i set file position indicator one line
upper in text file (line ends with "\n") ?

--
pozdr.
xEM~
Jul 17 '05 #1
5 14832
> how can i read for example 10 last lines from a text file beginning
from last line? do you have some idea?


One way:

$file = array_reverse( file( 'mine.txt' ) );

foreach ( $file as $line ){
echo "$line\n";
}

As for the second question, I'm not sure what you mean.
Jul 17 '05 #2
Sorry, I'm on crack:

$file = array_reverse( file( 'mine.txt' ) );
$limit = 10;

for ($i = 0; $i <= $limit; $i++ ){
echo $file[$i] . "\n";
}

Jul 17 '05 #3
> > how can i read for example 10 last lines from a text file beginning
from last line? do you have some idea?


Oops, you wanted 10 lines:

$file = array_reverse( file( 'mine.txt' ) );
$limit = 10;

for ($i = $limit; $i >= 0; $i-- ){
echo $file[$i] . "\n";
}

Jul 17 '05 #4
Maybe I should test these before I reply?
THIS ONE SPITS OUT TEN LINES :)

$file = array_reverse( file( 'mine.txt' ) );
$limit = 10;

for ($i = 0; $i < $limit; $i++ ){
echo $file[$i] . "\n";
}


Jul 17 '05 #5
On 11 Oct 2003 10:02:59 GMT, xEM <xe**@boxmail.biz> wrote:
how can i read for example 10 last lines from a text file beginning
from last line? do you have some idea?
There's more than one way to do it, but how about:

<pre>
<?php
$file = 'test.txt';

$fp = fopen($file, 'r');

if ($fp) {
$lines = array();

while (($line = fgets($fp)) !== false) {
$lines[] = $line;

while (count($lines) > 10)
array_shift($lines);
}

foreach (array_reverse($lines) as $line) {
print $line;
}

fclose($fp);
}
?>
</pre>

i.e. keep a running buffer of 10 lines (adding lines to the end, and shifting
them off the start if it exceeds 10 lines).

Then you said beginning from last line, so reverse the array before output.
in a different manner... how can i set file position indicator one line
upper in text file (line ends with "\n") ?


Use fseek and fgetc working backwards until you reach the \n you want?

http://uk2.php.net/fseek
http://uk2.php.net/manual/en/function.fgetc.php

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Erik Andersson | last post by:
Hi! I need to read a file (line-by-line) in reverse order, without reading the whole file into memory first. Is there an easy way of doing this? As in, is there a PHP function I could use? ...
35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
8
by: Jim Langston | last post by:
I have a class I designed that stores text chat in a std::vector<sd::string>. This class has a few methods to retrieve these strings to be displayed on the screen. void ResetRead( bool Reverse,...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
4
by: Jeff Rodriguez | last post by:
How would one go about reading a file line by line in reverse? For Example: -- FILE -- 1 2 3 4 5 -- FILE --
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
15
by: Fady Anwar | last post by:
Hi while browsing the net i noticed that there is sites publishing some software that claim that it can decompile .net applications i didn't bleave it in fact but after trying it i was surprised...
1
by: sunnyluthra1 | last post by:
Hi, I was creating an Application in MS Access for Geocoding a particular Address from Google to get the Lat & Long. I successfully able to did that. Here is the code:...
14
by: Faisal Shah | last post by:
Hi, Well, My name is Faisal, You might saw as my ID. I got into php before a few months... I need help.. I am trying to read a file that is .txt format.. Line by Line, Reverse action.. I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.