in reply to File output from end to beginning

This solution has been tested on both AIX and Win98. Its advantages are:
  1. only holds one line in memory, memory usage is the minimum
  2. only goes thru the file once, extreamly fast
One key point to help understand the source code: read from the beginning to the end, and write from the end to the beginning.

#usage perl -w test.pl oldfile newfile use Fcntl qw(SEEK_SET SEEK_END); use File::Copy; use strict; use constant NEWLINE_ADJUSTMENT => 0; #set this to 1 on Win98, 0 on un +ix! copy($ARGV[0], $ARGV[1]); open(AFILE, "<", $ARGV[0]); open(BFILE, "+<", $ARGV[1]); seek(AFILE, 0, SEEK_END); my $len = tell(AFILE); my $written_len = NEWLINE_ADJUSTMENT; seek(AFILE, 0, SEEK_SET); while (<AFILE>) { seek(BFILE, $len - $written_len - length(), SEEK_SET); $written_len += length() + NEWLINE_ADJUSTMENT; print BFILE; } close(AFILE); close(BFILE);