473,382 Members | 1,749 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,382 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 14828
> 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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.