Frank_Zappa_lives has asked for the wisdom of the Perl Monks concerning the following question:
# This is TAC # reverse of cat # # Take a list of files as command line parameters # and display the files on STDOUT # last file first # displaying each file backwards # The files are concatenated in 'normal' order to a temporary file # which is then read and displayed in reverse # check the number of parameters.. my $argCount = $#ARGV+1; if ($argCount<2) { exit 0; } # open the temporary file use Fcntl; use POSIX; my $outputFile; do { $outputFile = tmpnam(); } until sysopen(OUTPUTFILE, $outputFile, O_RDWR|O_CREAT|O_EXCL, 0666); # for every file.. while (@ARGV) { # open it my $fileName = shift; open ( MYFILE, " $fileName" ) or die "Cant open file $fileName"; # and write each line to the temporary file.. foreach (<MYFILE>) { print OUTPUTFILE $_; } close MYFILE; print OUTPUTFILE "\n"; print "Done $fileName -\n"; } close OUTPUTFILE; # now read it in backwards.. my $inputFile = $outputFile; open ( INPUTFILE, "< $inputFile"); my $char; my $fromEnd=1; my $fileSize = (stat INPUTFILE)[7]; # we seek to a position a distance from the end, and read a single cha +racter # the distance from end increases till we hit the start while ($fromEnd<$fileSize+1) { seek INPUTFILE, -$fromEnd, 2; # last char read INPUTFILE, $char, 1; print $char; ++$fromEnd; } close INPUTFILE; unlink $inputFile;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Grade me! TAC
by Joost (Canon) on Jun 11, 2004 at 11:01 UTC | |
by Frank_Zappa_lives (Initiate) on Jun 11, 2004 at 14:19 UTC | |
|
Re: Grade me! TAC
by gri6507 (Deacon) on Jun 11, 2004 at 13:16 UTC | |
by davido (Cardinal) on Jun 11, 2004 at 15:45 UTC | |
by runrig (Abbot) on Jun 11, 2004 at 16:05 UTC | |
by davido (Cardinal) on Jun 11, 2004 at 16:08 UTC | |
|
Re: Grade me! TAC
by bart (Canon) on Jun 11, 2004 at 10:53 UTC | |
|
Re: Grade me! TAC
by FoxtrotUniform (Prior) on Jun 11, 2004 at 16:36 UTC | |
|
Re: Grade me! TAC
by thens (Scribe) on Jun 12, 2004 at 12:08 UTC |