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
    That's correct: you cannot add something to the beginning of a file without rewriting the whole file.

    As it seems, the code does not implement one of the required tasks: 'then remove those 3 lines from daily.txt'.

    Of course, this can be fixed:

    #!/usr/bin/perl -w my $daily="daily.txt"; my $weekly="weekly.txt"; my $lines=3; # alternatively, read the names from the command line: # my($daily,$weekly,$lines)=@ARGV; # read the daily file: open DAILY, $daily or die "open $daily: $!"; my @daily=<DAILY>; close DAILY; # read the weekly file: open WEEKLY, $weekly or die "open $weekly: $!"; my @weekly=<WEEKLY>; close WEEKLY; # if the last line of @daily has no \n, this line will # be merged with the first line of @weekly. We are # paranoid and make sure it is there! $daily[-1]=~/\n$/ or $daily[-1].="\n"; # Well. That's good for Unix, not for Windows. You would # have to change "\n" to whatever is appropriate. # move the last $lines lines from the end of @daily to # the beginning of @weekly. Note: # splice(@daily, -$lines) removes and returns the last # $lines lines from @daily; # unshift @weekly, @some_lines adds some lines at the # beginning of @weekly. Together: unshift @weekly, splice(@daily, -$lines); # Write back the weekly file: open WEEKLY, ">$weekly" or die "open >$weekly: $!"; print WEEKLY @weekly; close WEEKLY; # Write back the daily file: open DAILY, ">$daily" or die "open >$daily: $!"; print DAILY @daily; close DAILY;

    You might be interested in the splice and unshift manpages for documentation of the main step of this program.

    By the way: It is possible to append to the end of a file by opening it with open FILE, ">>$name".

    With the truncate function, you can truncate a file (chop off the end leaving the beginning in place). However, I wouldn't recommend that: this function may not be implemented on some systems. Also, it's much simpler to overwrite the file.