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

Hi, I've a file which is opened for writing and then i close it. Again i open the same file for appending to its contents, but its not appending, its removing the older contents and freshly starting. Any help on whats the issue?
$tme=localtime(); my ($d,$m,$y) = (localtime)[3,4,5]; my $mdy1 = sprintf '%d-%d-%d', $d, $m+1, $y+1900; print "\nDate is:$mdy1\n"; if ($mdy1 eq "0-3-2015") {$mdy1 = "28-2-2015";} print "\nDate is:$mdy1\n"; $LOGFILE = "7252_Widevine_OemCrypto_Consolelog_User1"; $CONSOLE_LOGFILE = $LOGFILE.'_'.$mdy1.'.txt'; open CONSOLE_LOGF, ">", $CONSOLE_LOGFILE or die "Cannot open Console l +ogfile $CONSOLE_LOGFILE:$!"; system("pwd"); # Doing some operations here. close(CONSOLE_LOGF); $tme=localtime(); ($d,$m,$y) = (localtime)[3,4,5]; #my $mdy2 = sprintf '%d-%d-%d', $d-1, $m+1, $y+1900; my $mdy2 = sprintf '%d-%d-%d', $d, $m+1, $y+1900; print "\nDate is:$mdy2\n"; $LOGFILE1 = "7252_Widevine_OemCrypto_Consolelog_User1"; $CONSOLE_LOGFILE = $LOGFILE1.'_'.$mdy2.'.txt'; open(CONSOLE_LOGF, ">>", $CONSOLE_LOGFILE) || die "Cannot open Console + logfile $CONSOLE_LOGFILE: $!"; # ........... Here is where the previous contents are erased and new f +ile is created with new time(date is same here)..... system("pwd"); # Doing some operations here. close(CONSOLE_LOGF);
Thanks

Replies are listed 'Best First'.
Re: File append not working. Any help?
by choroba (Cardinal) on Jun 09, 2015 at 07:02 UTC
    Are you sure the file has any contents? Try adding a sleep after the first close and inspect it. Also, "some operations" might just leave the file intact, but we don't know, you haven't shown them.

    Are you sure localtime can return 0-3-2015? If so, shouldn't the check be present in the second part of the code, too? Also, when you use the same code in several places, think subroutine.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: File append not working. Any help?
by Anonymous Monk on Jun 09, 2015 at 06:58 UTC

    Hi, I've a file which is opened for writing and then i close it. Again i open the same file for appending to its contents, but its not appending, its removing the older contents and freshly starting. Any help on whats the issue?

    How do you know its not appending? Where do you check to see if its appending in your code?

    This won't go to the log  system("pwd");

    Also,

    Um, no

    $tme=localtime(); my ($d,$m,$y) = (localtime)[3,4,5]; my $mdy1 = sprintf '%d-%d-%d', $d, $m+1, $y+1900; print "\nDate is:$mdy1\n"; if ($mdy1 eq "0-3-2015") {$mdy1 = "28-2-2015";} print "\nDate is:$mdy1\n";

    Um, yes

    #!/usr/bin/perl -- use strict; use warnings; use Path::Tiny qw/ path /; use Time::Piece qw/ localtime /; use Data::Dump qw/ dd /; my $date = localtime()->strftime("%m-%d-%Y"); my $logprefix = "7252_Widevine_OemCrypto_Consolelog_User1"; my $logfile = $logprefix.'_'.$date.'.txt'; for my $ix ( 0 .. 3 ){ my $fh = path( $logfile )->opena_raw; print $fh "$ix\n"; close $fh; dd( path( $logfile )->slurp_raw ); } __END__ "0\n" "0\n1\n" "0\n1\n2\n" "0\n1\n2\n3\n"
      Hi Anonymous Monk, I did try your code, but its still not working. To answer your Q on how to check if its appending or not, I have a sleep command after opening the file for appending. Within that sleep, if i check the contents of the file, all contents are vanished! and it continues to write as if to a new file after the append statement. Any help. Thanks,

        Hi Anonymous Monk, I did try your code, but its still not working.

        what do you mean by that?

        To answer your Q on how to check if its appending or not, I have a sleep command after opening the file for appending. Within that sleep, if i check the contents of the file, all contents are vanished!

        Are you sure it had contents at any point?

Re: File append not working. Any help?
by james28909 (Deacon) on Jun 09, 2015 at 07:15 UTC
    Open it once with '+>' and then close it when your done writing any data to it. This will create the file, and then append to it until it is closed. If it is reopened, it will create a new file (deleting any existing data) and append to it until closed.
    use strict; use warnings; open (my $file, '+>', 'file.txt') or die "cannot open or append: $!"; print($file "test write on line number: ", sprintf("%2d", $_)) && prin +t ($file "\n") foreach (0..20);
Re: File append not working. Any help?
by sandy105 (Scribe) on Jun 09, 2015 at 08:58 UTC

    why dont you create a back of file first and check it

    $newname = "$CONSOLE_LOGFILE"."orig"; $cmd = "cp $CONSOLE_LOGFILE $newname"; system($cmd);
      use Path::Tiny; path($lf)->copy($bak);
Re: File append not working. Any help?
by Hosen1989 (Scribe) on Jun 09, 2015 at 19:02 UTC

    Hi there,

    I tried your code and add two print lines here to test it, and it looks fine to me!!!

    • print CONSOLE_LOGF "[1st print]\n";
    • print CONSOLE_LOGF "[2nd print]\n";

    And here the second print was appended to the first one.

    my next Q is: How do you write to these files, can you give some example?

    also you can see my version of your code here:

    $tme=localtime(); my ($d,$m,$y) = (localtime)[3,4,5]; my $mdy1 = sprintf '%d-%d-%d', $d, $m+1, $y+1900; print "\nDate is:$mdy1\n"; if ($mdy1 eq "0-3-2015") {$mdy1 = "28-2-2015";} print "\nDate is:$mdy1\n"; $LOGFILE = "7252_Widevine_OemCrypto_Consolelog_User1"; $CONSOLE_LOGFILE = $LOGFILE.'_'.$mdy1.'.txt'; open CONSOLE_LOGF, ">", $CONSOLE_LOGFILE or die "Cannot open Console l +ogfile $CONSOLE_LOGFILE:$!"; system("pwd"); print CONSOLE_LOGF "[1st print]\n"; #<<<<<<<<<<<<<<< # Doing some operations here. close(CONSOLE_LOGF); $tme=localtime(); ($d,$m,$y) = (localtime)[3,4,5]; #my $mdy2 = sprintf '%d-%d-%d', $d-1, $m+1, $y+1900; my $mdy2 = sprintf '%d-%d-%d', $d, $m+1, $y+1900; print "\nDate is:$mdy2\n"; $LOGFILE1 = "7252_Widevine_OemCrypto_Consolelog_User1"; $CONSOLE_LOGFILE = $LOGFILE1.'_'.$mdy2.'.txt'; open(CONSOLE_LOGF, ">>", $CONSOLE_LOGFILE) || die "Cannot open Console + logfile $CONSOLE_LOGFILE: $!"; # ........... Here is where the previous contents are erased and new f +ile is created with new time(date is same here)..... system("pwd"); print CONSOLE_LOGF "[2nd print]\n"; #<<<<<<<<<<<<<<< # Doing some operations here. close(CONSOLE_LOGF);