File Append Issue

Discussion in 'Perl Misc' started by Anand, Nov 13, 2003.

  1. Anand

    Anand Guest

    After looking at a number of sources which have little to say on appending
    to a file I came up with the following piece of code to append a sentence to
    a file:

    #!/usr/bin/perl

    #open(FILE, ">>test1.html") || die $!;
    #print FILE "This is a test line added to the file, <br>\n";

    $line_to_write="I've appended a line to a file !";
    $file_name="test1.html";

    open(DAT,">>$file_name");

    print DAT "$line_to_write\n";

    close(DAT);

    The commented and uncommented sections do the same thing though, they create
    a second file without the .html extender rather than appending to
    test1.html. What am I doing wrong here?

    A.
     
    Anand, Nov 13, 2003
    #1
    1. Advertising

  2. Anand

    Kasp Guest

    "Anand" <> wrote in message
    news:f1Msb.33513$...
    > The commented and uncommented sections do the same thing though, they

    create
    > a second file without the .html extender rather than appending to
    > test1.html. What am I doing wrong here?


    Try specifying the absolute path to the file test1.html

    Also remember to "close" the file once you are done working with it.

    Add the following two lines to your code at the top:
    use strict;
    use warnings;

    --
     
    Kasp, Nov 13, 2003
    #2
    1. Advertising

  3. Anand

    Anand Guest

    "Kasp" <> wrote in message
    news:bp02se$1j6der$-berlin.de...
    > "Anand" <> wrote in message
    > news:f1Msb.33513$...
    > > The commented and uncommented sections do the same thing though, they

    > create
    > > a second file without the .html extender rather than appending to
    > > test1.html. What am I doing wrong here?

    >
    > Try specifying the absolute path to the file test1.html
    >
    > Also remember to "close" the file once you are done working with it.
    >
    > Add the following two lines to your code at the top:
    > use strict;
    > use warnings;
    >

    Kasp,

    I made the changes recommended but the line indicated below errors with the
    following:
    print() on closed filehandle DAT at F:\Perl_Work\fileappend.pl line 15.

    I presume that it sees the file as being closed when attempting to append
    and thus writes a new file but why?

    my $line_to_write="I've appended a line to a file !";
    my $file_name="c:\test1.html";

    open DAT,">>$file_name";

    print DAT "$line_to_write\n"; #this line errors

    close DAT;

    A.
     
    Anand, Nov 13, 2003
    #3
  4. Anand

    Kasp Guest

    Problem was with the way $file_name is defined. A \ in double quotes gets
    interpolated.
    The "c:\test.htm" is tansformed to "c:<TAB>est.htm" .... so just use /
    instead of \ for file names, paths etc.

    Here is the code that worked for me:

    use strict;
    use warnings;

    my $line_to_write="I've appended a line to a file !";
    my $file_name="c:/test1.html"; # Used / instead of \ .... \t means TAB

    open DAT,">>$file_name";

    print DAT "$line_to_write\n"; #this line errors

    close DAT;
     
    Kasp, Nov 13, 2003
    #4
  5. Anand <> wrote:

    > open DAT,">>$file_name";



    You should always, yes *always*, check the return value from open():

    open DAT,">>$file_name" or die "could not open '$file_name' $!";


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 13, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. uy_do
    Replies:
    1
    Views:
    674
    Harish Madhavan
    Dec 4, 2003
  2. Dilantha Seneviratne

    How to append to a text file

    Dilantha Seneviratne, Jan 5, 2004, in forum: Java
    Replies:
    10
    Views:
    9,874
    Andrew Thompson
    Jan 9, 2004
  3. Dustin

    FileOutputStream append issue

    Dustin, Jan 3, 2007, in forum: Java
    Replies:
    7
    Views:
    1,229
    Mark Thornton
    Jan 4, 2007
  4. HYRY
    Replies:
    10
    Views:
    612
    Bruno Desthuilliers
    Sep 26, 2007
  5. PerlFAQ Server
    Replies:
    0
    Views:
    390
    PerlFAQ Server
    Feb 24, 2011
Loading...

Share This Page