Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: How do I append or write to the end of a file?

by punksk8er (Initiate)
on Dec 17, 2000 at 23:09 UTC ( [id://47138]=note: print w/replies, xml ) Need Help??


in reply to How do I append or write to the end of a file?

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.

Replies are listed 'Best First'.
Re: Answer: How do I append or write to the end of a file?
by chipmunk (Parson) on Dec 17, 2000 at 23:47 UTC
    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.

Re: Answer: How do I append or write to the end of a file?
by choroba (Cardinal) on Aug 28, 2013 at 11:54 UTC
    The answer is wrong. The last three prints do not go into the output file.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://47138]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-16 05:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found