Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by vroom (His Eminence)
on Jan 08, 2000 at 08:46 UTC ( #1882=perlquestion: print w/replies, xml ) Need Help??

vroom has asked for the wisdom of the Perl Monks concerning the following question: (files)

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

Originally posted as a Categorized Question.

  • Comment on How do I append or write to the end of a file?

Replies are listed 'Best First'.
Re: How do I append or write to the end of a file?
by Crulx (Monk) on Jan 21, 2000 at 04:59 UTC

    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";
Re: How do I append or write to the end of a file?
by ravi6289 (Initiate) on Aug 28, 2013 at 11:31 UTC
    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.

Re: How do I append or write to the end of a file?
by punksk8er (Initiate) on Dec 17, 2000 at 23:09 UTC
    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.
      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.

      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: perlquestion [id://1882]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2022-12-05 09:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?