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.


In reply to Re: Answer: How do I append or write to the end of a file? by chipmunk
in thread How do I append or write to the end of a file? by vroom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.