http://www.perlmonks.org?node_id=667868

sudeep has asked for the wisdom of the Perl Monks concerning the following question:

hi,

here i am trying to append the data to a file with the same syntax before it was working fine after sligh modifications of the code it is not writing or appending the data to a file i dont understand why its happening here is my code

open (writefile,">>",$target_full_path) || die "Can't open $target_full_path"; while($fetchdata=$sth4->fetchrow_array()) { print $fetchdata; #it is working print writefile $fetchdata; # not appending $rowcount++; #here it is counting } close writefile; print $rowcount;
here it is fetching the data and in the while loop i used "print $fetchdata" to check whether it is retreiving teh data or not here it is fetching the data but i dont know why its not appending into a file. Any suggestions would be great.

Thanks

Replies are listed 'Best First'.
Re: appending data to a file
by kyle (Abbot) on Feb 14, 2008 at 03:11 UTC

    Here's the code as I've deciphered it:

    open (writefile,">>",$target_full_path) || die "Can't open $target_ful +l_path"; while($fetchdata=$sth4->fetchrow_array()) { print $fetchdata; #it is working print writefile $fetchdata; # not appending $rowcount++; #here it is counting } close writefile; print $rowcount;

    When you open, it doesn't die? Maybe try printing $target_full_path to make sure it has what you think it does. Your argument to die should include $! in it somewhere since that will have the error that caused open to fail.

    I'm sorry, I don't see why this wouldn't work. What was the slight modification that causes it to break?

      Are you really trying to capture the return value of $sth->fetchrow_array() in a scalar? Your comments imply that you're seeing data on STDOUT from the first print $fetchdata;, but I don't see how it could be the data you wanted. The DBI docs suggest:

      while ( @row = $sth->fetchrow_array ) { print "@row\n"; }

      That said, it seems to me that you'd likely run into trouble if the return value were ever an empty string ("") or something else that evaluates to FALSE.

      Hi,
        I think this code will work. Check it..
      open (writefile,">>$target_full_path") || die "Can't open $target_full +_path"; while($fetchdata=$sth4->fetchrow_array()) { print $fetchdata; print writefile $fetchdata; $rowcount++; #here it is counting } close writefile; print $rowcount;
      Cheers !
        Bipin Balan
Re: appending data to a file
by graff (Chancellor) on Feb 14, 2008 at 03:25 UTC
    Here's a suggestion, just to make sure you are looking at the right evidence when you say that the append operation is not working on the file (I'm also fixing your indentation a bit, and improving the error checking/reporting):
    my $original_size = -s $target_full_path; # how many bytes in the fil +e open( my $out, ">>", $target_full_path) # I like using lexical file + handles, and or die "Can't open $target_full_path: $!"; # reporting system erro +r messages while($fetchdata=$sth4->fetchrow_array()) { print $fetchdata; print $out $fetchdata or die "print to file failed: $!"; $rowcount++; } close $out or die "Unable to close output file: $!"; print $rowcount; my $current_size = -s $target_full_path; # how many bytes now printf( "File %s was %d bytes, is now %d bytes\n", $target_full_path, $original_size, $current_size );
    Maybe the reports you get from that version will tell you what you need to know.
Re: appending data to a file
by ww (Archbishop) on Feb 14, 2008 at 03:14 UTC

    You made a significant start toward finding your problem when you discovered what you say in your comment "not appending." That kind of output error -- after some data acquisition you say is working -- is a pretty broad hint that you should look to your filehandling.

    So, for starters, you might have consulted the documentation (perldoc -f open) to check the syntax of open-append:

    open (writefile, ">>,$target_full_path") { # quoting changed!

    I'd also suggest you change writefile to upper case (or to FH which is one conventional form) to avoid a possible clash with a future reserved word. (use warnings;</c> would have told you this. Well, it would have warned about the possible issue.)

    Have you predeclared $rowcount (as a global?). ...$sth4?

    Have you considered the benefits of using strict and warnings to help solve questions like this? I'm not clear what kind prior version that would have morphed to this with "slight modifications" COULD have been working.

    And, FWIW, a simplified version that does work (with your comments preserved):

    use strict; use warnings; my $rowcount = 0; open (FH, ">> 667868.txt") || die "Can't open 667868.txt $!"; while(<DATA>) { print $_; # it is working print FH $_; # not appending $rowcount++; # here it is counting } close FH; print $rowcount; __DATA__ now is the time for all good men to come to the aid of their country, while the quick red fox jumps over the lazy brown dog's back.

    And, finally, please use <c>...<</c> tags around code. See Writeup Formatting Tips.

    UpdateTwo days later: comment re quoting added with thanks to graff for pointing out that OP quoting was OK.

      So, for starters, you might have consulted the documentation (perldoc -f open) to check the syntax of open-append:
      open (writefile, ">>,$target_full_path") {

      Um, the OP actually started with:

      open (writefile, ">>",$target_full_path) {
      which is a perfectly acceptable use of the 5.8 "three-arg open" calling style, so that's not the problem, and there's no need to "fix" it. (I would object to the use of an all-lower-case bareword for the file handle -- either make ALLCAPS or else use a "my" variable for that -- but that's a separate matter.)
Re: appending data to a file
by roboticus (Chancellor) on Feb 14, 2008 at 03:00 UTC
    sudeep:

    Just slightly unmodify your code to get it working again ;^P

    If you use code tags (<c>code</c>) to format your code it would be easier to help.

    ...roboticus