Tim Hammerquist wrote:
>
> "Elston, Jeremy" wrote:
> >
> > Greetings...
> >
> > Peter's solution is the one I would recommend unless you are reading large
> > files and/or your system has little memory available. By reading into an
> > array, the entire file will be pulled into memory. If memory use is an
> > issue, you could use something like this:
> >
> > -----
> >
> > open(FILE, "my_file.txt") or die;
> >
> > $pos = -2; # Use this to get past EOF and last newline
>
> This is non-portable. eg, DOS/Win32 uses a 2 byte line terminator
> ("\r\n" or "\015\012").
>
> >
> > while($char ne "\n")
> > {
> > seek FILE, $pos, 2;
> > read FILE, $char, 1;
> > $pos--;
> > }
> >
> > $final = <FILE>;
> > print "Last line is: $final\n";
It's fine if you use binmode to keep the \r from sucking in the \n:
use stric;
use IO::Seekable;
open FILE, 'some.txt' or die;
binmode FILE;
my $pos = -2; # back up before last \n
my $char = '';
while ($char ne "\n") {
seek FILE, $pos, SEEK_END;
read FILE, $char, 1;
print "char read: ", ord $char, "\n";
$pos--;
}
my $final = <FILE>;
print "Last line is: $final\n";
There's also a possiblity that the last char isn't a \n which you could check for.
--
,-/- __ _ _ $Bill Luebkert ICQ=14439852
(_/ / ) // // DBE Collectibles http://www.todbe.com/
/ ) /--< o // // Mailto:[EMAIL PROTECTED] http://dbecoll.webjump.com/
-/-' /___/_<_</_</_ http://www.freeyellow.com/members/dbecoll/
_______________________________________________
Perl-Unix-Users mailing list. To unsubscribe go to
http://listserv.ActiveState.com/mailman/subscribe/perl-unix-users