in reply to Reading and writing to files
Inserting lines into text files can only be done by rewriting the text files. You will have to create a new file, write the three lines into it and then append monthly.txt to that file, and then rename that file to monthly.txt.
Update: See the below node by Anonymous Monk for stuff I left out and additional hints.
Some untested code :
#/usr/bin/perl -w use strict; my $daily = "daily.txt"; my $monthly = "monthly.txt"; # These will hold all lines from the files my @daily_lines; my @monthly_lines; # First extract the last three lines from $daily local *DAILY; open DAILY, "< $daily" or die "Can't open $daily: $!\n"; @daily_lines = <DAILY>; close DAILY; @daily_lines = @daily_lines[-3..-1]; # Now, @daily_lines contains the three last lines from the # file named $daily # Now we will recreate the monthly file : local *MONTHLY; open MONTHLY, "< $monthly" or die "Can't open $monthly : $!\n"; @monthly_lines = <MONTHLY>; close MONTHLY; # And write the new file : open MONTHLY, "> $monthly.tmp"; print MONTHLY join "", @daily_lines; print MONTHLY join "", @monthly_lines; close MONTHLY; # Now we delete $monthly and move our temp file over it : unlink $monthly or die "Couldn't remove old file $monthly : $!\n"; rename( "$monthly.tmp", "$monthly" ) or die "Couldn't rename $monthly. +tmp to $monthly : $!\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Re: Reading and writing to files
by Anonymous Monk on Aug 09, 2000 at 14:40 UTC |