473,386 Members | 1,798 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,386 software developers and data experts.

Reading a file in reverse order

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
Jul 17 '05 #1
14 13650
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

Jul 17 '05 #2
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


Jul 17 '05 #3
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
Jul 17 '05 #4
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



Jul 17 '05 #5
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
Jul 17 '05 #6
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
Jul 17 '05 #7
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
Jul 17 '05 #8
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
Jul 17 '05 #9
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

Jul 17 '05 #10
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

Jul 17 '05 #11
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


Jul 17 '05 #12
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


Jul 17 '05 #13
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

Jul 17 '05 #14
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-

Jul 17 '05 #15

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

Similar topics

3
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...
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....
8
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;
15
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
10
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...
3
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...
2
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...
6
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...
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: 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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.