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?
The solution has to be platform independent, so "popen("tac $filename")...
wouldn't work.
Example input and ouput:
<myfile.txt input>
line 1
line 2
line 3
</myfile.txt input>
<output from php script>
line 3
line 2
line 1
</output from php script>
All the best,
Erik
--
Erik Andersson ( AussieTraders.com.au ) http://www.aussietraders.com.au/
- Placing an add is FREE at AussieTraders.com.au, Australia's buy & sell
marketplace 14 13579
if the files are small, load them into an array $aryFile =
file("filename.txt");
then just reverse the array or read backwords
--
Mike Bradley http://www.gzentools.com -- free online php tools
"Erik Andersson" <er**@eneit.com> wrote in message
news:40*********************@uq-127creek-reader-01.brisbane.pipenetworks.com
..au... 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?
The solution has to be platform independent, so "popen("tac $filename")... wouldn't work.
Example input and ouput:
<myfile.txt input> line 1 line 2 line 3 </myfile.txt input>
<output from php script> line 3 line 2 line 1 </output from php script>
All the best,
Erik
-- Erik Andersson ( AussieTraders.com.au ) http://www.aussietraders.com.au/ - Placing an add is FREE at AussieTraders.com.au, Australia's buy & sell marketplace
They're not, hence "without reading the whole file into memory first".
Thanks anyway!
--
Erik Andersson ( AussieTraders.com.au ) http://www.aussietraders.com.au/
- Placing an add is FREE at AussieTraders.com.au, Australia's buy & sell
marketplace
"CountScubula" <me@scantek.hotmail.com> wrote in message
news:36*******************@newssvr25.news.prodigy. com... if the files are small, load them into an array $aryFile = file("filename.txt");
then just reverse the array or read backwords
-- Mike Bradley http://www.gzentools.com -- free online php tools "Erik Andersson" <er**@eneit.com> wrote in message
news:40*********************@uq-127creek-reader-01.brisbane.pipenetworks.com .au... 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?
The solution has to be platform independent, so "popen("tac
$filename")... wouldn't work.
Example input and ouput:
<myfile.txt input> line 1 line 2 line 3 </myfile.txt input>
<output from php script> line 3 line 2 line 1 </output from php script>
All the best,
Erik
-- Erik Andersson ( AussieTraders.com.au ) http://www.aussietraders.com.au/ - Placing an add is FREE at AussieTraders.com.au, Australia's buy & sell marketplace
On 2004-01-29, Erik Andersson <er**@eneit.com> wrote: 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?
There are some nice GNU-tools:
With cat you get the contents of a file from the beginning to the end
With tac you get the contents of a file from the end to the beginning ;)
-- http://home.mysth.be/~timvw
Ah yes, sorry, next time I should read more clearly
--
Mike Bradley http://www.gzentools.com -- free online php tools
"Erik Andersson" <er**@eneit.com> wrote in message
news:40*********************@uq-127creek-reader-01.brisbane.pipenetworks.com
..au... They're not, hence "without reading the whole file into memory first".
Thanks anyway!
-- Erik Andersson ( AussieTraders.com.au ) http://www.aussietraders.com.au/ - Placing an add is FREE at AussieTraders.com.au, Australia's buy & sell marketplace
"CountScubula" <me@scantek.hotmail.com> wrote in message news:36*******************@newssvr25.news.prodigy. com... if the files are small, load them into an array $aryFile = file("filename.txt");
then just reverse the array or read backwords
-- Mike Bradley http://www.gzentools.com -- free online php tools "Erik Andersson" <er**@eneit.com> wrote in message
news:40*********************@uq-127creek-reader-01.brisbane.pipenetworks.com .au... 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?
The solution has to be platform independent, so "popen("tac $filename")... wouldn't work.
Example input and ouput:
<myfile.txt input> line 1 line 2 line 3 </myfile.txt input>
<output from php script> line 3 line 2 line 1 </output from php script>
All the best,
Erik
-- Erik Andersson ( AussieTraders.com.au ) http://www.aussietraders.com.au/ - Placing an add is FREE at AussieTraders.com.au, Australia's buy &
sell marketplace
Tim Van Wassenhove wrote: With cat you get the contents of a file from the beginning to the end With tac you get the contents of a file from the end to the beginning ;)
Those don't exist on Windows though.
Does fopen() read it into memory?
Chris
--
Chris Jenkinson
On 2004-01-29, Chris Jenkinson <ta*****@talrias.net> wrote: Tim Van Wassenhove wrote: With cat you get the contents of a file from the beginning to the end With tac you get the contents of a file from the end to the beginning ;)
Those don't exist on Windows though.
Offcourse they do: http://unxutils.sourceforge.net/
-- http://home.mysth.be/~timvw
Tim Van Wassenhove wrote: On 2004-01-29, Chris Jenkinson <ta*****@talrias.net> wrote:
Tim Van Wassenhove wrote:
With cat you get the contents of a file from the beginning to the end With tac you get the contents of a file from the end to the beginning ;)
Those don't exist on Windows though.
Offcourse they do:
http://unxutils.sourceforge.net/
Some people won't be in a position to download them and use them, for
any number of reasons. The idea solution would do this without having to
download extra files.
--
Chris Jenkinson
On 2004-01-29, Chris Jenkinson <ta*****@talrias.net> wrote: Some people won't be in a position to download them and use them, for any number of reasons. The idea solution would do this without having to download extra files.
With fseek you can do positioning of the file pointer (fp). Just put the
fp at the end of the file. Read the char. Position the pointer at
feof-1, read the char. postion at feof-2, read the char, ...
-- http://home.mysth.be/~timvw
ok, got it,
open the file, then have a function like revLineRead()
and in this function, set the postion marker to 1k below the end of file,
read in 1k into a string, say $inBuffer,
scan $inBuffer up to the first /n and save this in $nextBuffer, and remove
from $inBuffer.
now $aryLines = explode("\n",$inBuffer);
ok, to read last line:
$r = ary_pop($aryLines);
if $r == "" then we read in another 1k and put $nextBuffer at end of
$inBuffer, then do recan for first /n
this should do it.
--
Mike Bradley http://www.gzentools.com -- free online php tools
"Erik Andersson" <er**@eneit.com> wrote in message
news:40*********************@uq-127creek-reader-01.brisbane.pipenetworks.com
..au... 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?
The solution has to be platform independent, so "popen("tac $filename")... wouldn't work.
Example input and ouput:
<myfile.txt input> line 1 line 2 line 3 </myfile.txt input>
<output from php script> line 3 line 2 line 1 </output from php script>
All the best,
Erik
-- Erik Andersson ( AussieTraders.com.au ) http://www.aussietraders.com.au/ - Placing an add is FREE at AussieTraders.com.au, Australia's buy & sell marketplace
Sure sounds like a homework assignment to me! Just curious what the
application may be.
"Erik Andersson" <er**@eneit.com> wrote in message
news:40*********************@uq-127creek-reader-01.brisbane.pipenetworks.com.au... 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?
The solution has to be platform independent, so "popen("tac $filename")... wouldn't work.
Example input and ouput:
<myfile.txt input> line 1 line 2 line 3 </myfile.txt input>
<output from php script> line 3 line 2 line 1 </output from php script>
All the best,
Erik
-- Erik Andersson ( AussieTraders.com.au ) http://www.aussietraders.com.au/ - Placing an add is FREE at AussieTraders.com.au, Australia's buy & sell marketplace
Proposed solution below.
I'm fairly new to PHP and very new to OO so please forgive/Point out any
issues/problems
<?php
/************************************************** ***
** Title.........: RevFile Class
** Version.......: 1.00
** Author........: Steve Weet <sw***@weet.demon.co.uk>
** Filename......: class.RevFile.php
** Last changed..: 30th Jan 2004
** Purpose.......: Allows the display of a file in
** ..............: In reverse order
************************************************** ****/
// Example Usage
$file = new RevFile("/etc/passwd");
while ( ! $file->sof() ) {
echo $file->GetLine() ;
}
class RevFile {
var $FileName;
var $FileHandle;
var $FilePos;
function RevFile($filename) {
$this->FileName = $filename;
$this->FileHandle = @fopen($filename, "r") or
die("Could not open file $filename\n");
// Find EOF
if ( ! (fseek($this->FileHandle, 0, SEEK_END ) == 0 ))
die ("Could not find end of file in $filename\n");
// Store file position
$this->FilePos = ftell($this->FileHandle);
// Check that file is not empty or doesn;t contain a single newline
if ($this->FilePos < 2 )
die ("File is empty\n");
// Position file pointer just before final newline
// i.e. Skip EOF
$this->FilePos -= 1;
}
function GetLine() {
$pos = $this->FilePos -1;
$ch=" ";
$line = "";
while ($ch != "\n" && $pos >= 0) {
fseek($this->FileHandle, $pos );
$ch = fgetc($this->FileHandle);
// Decrement out pointer and prepend to the line
// if we have not hit the new line
if ( $ch != "\n" ) {
$pos = $pos -1;
$line = $ch . $line;
}
}
$this->FilePos = $pos ;
return $line . "\n";
}
function sof() {
return ($this->FilePos <= 0 );
}
}
?>
Erik Andersson wrote: 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?
The solution has to be platform independent, so "popen("tac $filename")... wouldn't work.
Example input and ouput:
<myfile.txt input> line 1 line 2 line 3 </myfile.txt input>
<output from php script> line 3 line 2 line 1 </output from php script>
All the best,
Erik
-- Erik Andersson ( AussieTraders.com.au ) http://www.aussietraders.com.au/ - Placing an add is FREE at AussieTraders.com.au, Australia's buy & sell marketplace
It's not... I'm building a homegrown web log analyser which pulls all new
entries in a log file into the database. The database shouldn't have any
duplicates, hence I need to read from the bottom instead of the top when
parsing the log file. It would just be a waste of time reading a massive log
file from the top as all the new entries are at the bottom.
--
Erik Andersson ( AussieTraders.com.au ) http://www.aussietraders.com.au/
- Placing an add is FREE at AussieTraders.com.au, Australia's buy & sell
marketplace
"Theorem" <t h e o r e m @ a x i o m e t r i c . o r g> wrote in message
news:10*************@news.supernews.com... Sure sounds like a homework assignment to me! Just curious what the application may be.
"Erik Andersson" <er**@eneit.com> wrote in message
news:40*********************@uq-127creek-reader-01.brisbane.pipenetworks.com
..au... 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?
The solution has to be platform independent, so "popen("tac
$filename")... wouldn't work.
Example input and ouput:
<myfile.txt input> line 1 line 2 line 3 </myfile.txt input>
<output from php script> line 3 line 2 line 1 </output from php script>
All the best,
Erik
-- Erik Andersson ( AussieTraders.com.au ) http://www.aussietraders.com.au/ - Placing an add is FREE at AussieTraders.com.au, Australia's buy & sell marketplace
Works absolutely perfect for what I'm using it for! Thanks, alot!
I'm not sure as for what efficiency `fgetc` has, but for this project speed
is not that important.
All the best,
Erik Andersson ( AussieTraders.com.au ) http://www.aussietraders.com.au/
- Placing an add is FREE at AussieTraders.com.au, Australia's buy & sell
marketplace
"Steve Weet" <sw***@dciltd.com> wrote in message
news:40**************@dciltd.com... Proposed solution below.
I'm fairly new to PHP and very new to OO so please forgive/Point out any issues/problems
<?php /************************************************** *** ** Title.........: RevFile Class ** Version.......: 1.00 ** Author........: Steve Weet <sw***@weet.demon.co.uk> ** Filename......: class.RevFile.php ** Last changed..: 30th Jan 2004 ** Purpose.......: Allows the display of a file in ** ..............: In reverse order ************************************************** ****/
// Example Usage $file = new RevFile("/etc/passwd");
while ( ! $file->sof() ) { echo $file->GetLine() ; }
class RevFile {
var $FileName; var $FileHandle; var $FilePos;
function RevFile($filename) {
$this->FileName = $filename;
$this->FileHandle = @fopen($filename, "r") or die("Could not open file $filename\n");
// Find EOF if ( ! (fseek($this->FileHandle, 0, SEEK_END ) == 0 )) die ("Could not find end of file in $filename\n");
// Store file position $this->FilePos = ftell($this->FileHandle);
// Check that file is not empty or doesn;t contain a single
newline if ($this->FilePos < 2 ) die ("File is empty\n");
// Position file pointer just before final newline // i.e. Skip EOF $this->FilePos -= 1; } function GetLine() { $pos = $this->FilePos -1; $ch=" "; $line = ""; while ($ch != "\n" && $pos >= 0) { fseek($this->FileHandle, $pos ); $ch = fgetc($this->FileHandle);
// Decrement out pointer and prepend to the line // if we have not hit the new line if ( $ch != "\n" ) { $pos = $pos -1; $line = $ch . $line; } } $this->FilePos = $pos ; return $line . "\n"; }
function sof() { return ($this->FilePos <= 0 ); } } ?>
Erik Andersson wrote: 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?
The solution has to be platform independent, so "popen("tac
$filename")... wouldn't work.
Example input and ouput:
<myfile.txt input> line 1 line 2 line 3 </myfile.txt input>
<output from php script> line 3 line 2 line 1 </output from php script>
All the best,
Erik
-- Erik Andersson ( AussieTraders.com.au ) http://www.aussietraders.com.au/ - Placing an add is FREE at AussieTraders.com.au, Australia's buy & sell marketplace
Erik Andersson wrote: It's not... I'm building a homegrown web log analyser which pulls all new entries in a log file into the database. The database shouldn't have any duplicates, hence I need to read from the bottom instead of the top when parsing the log file. It would just be a waste of time reading a massive log file from the top as all the new entries are at the bottom.
This is a search problem in a sorted collection. You should search the
last processed entry in a collection of ordered records (entries in web
log). I think that you can find some example of find algoritms on the web.
Another way (more performant) can be to store the last processed entry
position, and the next time you should parse the file do a seek at the
old position. You should put some control and handle exception but IMHO
this is the correct way.
Regards.
-Ema- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: James Lee |
last post by:
I am doing a simple query like col1, col2, date, col4 from table1.
All four colums are of type blob. For date column, I store a string
like this:
Fri Feb 13 11:01:24 2004
I store records as...
|
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....
|
by: vijay |
last post by:
Hello,
As the subject suggests, I need to print the string in the reverse
order. I made the following program:
# include<stdio.h>
struct llnode
{
char *info;
|
by: Fabio Cannizzo |
last post by:
Is it possible to do something similar to the STL iterators, i.e. to execute
a foreach loop in reverse order?
Thanks,
Fabio
|
by: aatish19 |
last post by:
1: Write a program that asks the user to input an integer value and
print it in a reverse order
Sample Output
Enter any number 65564
Reverse of 65564 is 46556
2: Write a program that takes a...
|
by: thrill5 |
last post by:
I have an xml document such as:
<device>
<element>first</element>
<element>second</element>
</device>
I am using this as the source for an xslt transform that goes like
<xsl:for-each...
|
by: srp8982 |
last post by:
hello,
I need help (code) on following program ,its in c...
1)allow user to write file name in command line, read the file...
2) count characters, spaces and no. of lines in file given...
3)read...
|
by: aburningflame23 |
last post by:
alright this is my problem.....please help
i need to write a program that reads 10 integers from a file into an array and then prints out the numbers in reverse order. It also prints out the...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |