open FH, ">>$output_file" or die "Couldn't open file: $!";
Opens a file for appending.
In an already open file for writing,
seek FH,0,2;
After you get to the end via those methods, simply
print FH "What you want to print\n";
| [reply] [d/l] |
Best thing would be is to open the file in the append mode ,, Not a fan of the previous answer , for one reason that it will not work :
!/usr/bin/perl
use strict;
open (FILE, "file") || die "File does not exist\n";
my @file = <FILE>;
my $file = @file;
close (FILE);
open (FILE, ">file") || die "File does not exist\n";
for (my $n = 0; $n <= $file; $n++) {
$_ = $file[$n];
print FILE $_;
}
print "add whatever"; --> should be print FILE "add whatever";
print "here\n"; --> should be print FILE "here\n";
print "to end of txt.\n"; --> should be print FILE "to end of txt.\n";
close (FILE);
Originally posted as a Categorized Answer. | [reply] [d/l] |
Another mehod of doing so is a bit longer, but of which I prefer at the moment.
#!/usr/bin/perl -w
use strict;
open (FILE, "file") || die "File does not exist\n";
my @file = <FILE>;
my $file = @file;
close (FILE);
open (FILE, ">file") || die "File does not exist\n";
for (my $n = 0; $n <= $file; $n++) {
$_ = $file[$n];
print FILE $_;
}
print "add whatever";
print "here\n";
print "to end of txt.\n";
close (FILE);
I prefer this method because I do not have to make extream changes to edit the lines of the file as I go. | [reply] [d/l] |
There are a number of things in this program that could be improved. The use of -w and use strict is very good, of course.
When checking the result of open() and similar functions, you should print $!, which contains the system error message. You're printing "File does not exist", when the open could have failed for some other reason, such as the wrong permissions on the file.
open(FILE, "file") or die "Cannot open file for reading: $!\n";
In the for loop, you copy $file[$n] to $_ and then print $_, instead of printing $file[$n] directly.
print FILE $file[$n];
But your for loop is written for C, rather than for Perl. This loop should be written with foreach instead. (Making it appropriate to print $_ again.)
foreach (@file) {
print FILE $_;
}
Your three additional print statements can be combined into one print statement (that should print to FILE rather than STDOUT) using a here doc.
print FILE <<EOT;
add whatever here
to end of txt.
EOT
Now the program looks like this:
#!/usr/bin/perl -w
use strict;
open(FILE, "file") or die "Can't open file for reading: $!\n";
my @file = <FILE>;
close(FILE);
open(FILE, ">file") or die "Can't open file for writing: $!\n";
foreach (@file) {
print FILE $_;
}
print <<EOT;
add whatever here
to end of txt.
EOT
close(FILE);
Of course, other improvements could be made, like printing the whole array at once, but I'm keeping in mind your remark that this structure allows you to make minimal changes to the code to edit the lines when you print them back out.
Places to look for more info on the suggestions I made: perlvar for $!; print; perlsyn for foreach loops; perldata for here-docs. | [reply] [d/l] [select] |
The answer is wrong. The last three prints do not go into the output file.
| [reply] |