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

Hi,

Need help to fix the below error.

I m trying to write a file twice in the same script. Both the time i m trying to append the o/p file

$file = aaa.txt first time file mode open( my $testfile, "+>> $file" ) $!\n"; if ( -z "$testfile" ) ---> checking for empty file { write some o/p data close( $testfile );

second time time file process

open( my $testfile, "+>> $file" ) $!\n"; ---- ---- some condition check now adding some other info to the existing file close the file

Error:

First read is working for second read unable to open the file and appending the records.

Replies are listed 'Best First'.
Re: Applying multiple file modes
by Athanasius (Archbishop) on May 25, 2015 at 03:33 UTC

    Hello leelu, and welcome to the Monastery!

    Immediately after an appending operation, the file pointer is positioned at the end of the file, so a read operation will always return nothing. You first have to seek to the position where you want to begin reading. The following script is my take on what (I think) you are trying to do:

    #! perl use strict; use warnings; use Fcntl ':seek'; my $file = 'aaa.txt'; if (!-e $file || -z $file) { # print "File '$file' is empty\n"; open(my $fh1, '>', $file) or die "Cannot open file '$file' for writing: $!"; print $fh1 "The quick brown fox jumped over the unfortunate dog.\n +"; close $fh1 or die "Cannot close file '$file': $!"; } open(my $fh2, '+>>', $file) or die "Cannot open file '$file' for appending and reading: $!"; print $fh2 "We have nothing to fear but fear itself.\n"; seek $fh2, 0, SEEK_SET; print while <$fh2>; close $fh2 or die "Cannot close file '$file': $!";

    Notes:

    1. Like all file test functions, -z returns undef (a false value) if the file does not exist. So you need to test for existence as well as zero size.

    2. If you are writing to the file only if it is empty, then appending (>>) is the same as simply writing (>).

    3. SEEK_SET is defined in the Fcntl module. It specifies the start of the file as the position at which the offset (in this case, 0) is applied.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thanks for the response, but i have tried with the given solution but getting error during while condition. Plse check the below ++++++++++++++++++++++++++++++++++++
      use strict; use warnings; my $file = 'aaa.txt'; if ( !-e $file || -z $file ) { open( my f1,'>', $file ) or die "unable to open output log file: $resu +lts. Error: $!\n"; print James|chicago "\n"; close( $f1 ); #close output file handle } else { open( my $f2, '+>>', $file ) or die "unable to open output log file: $ +results. Error: $!\n"; while (<$f2>) { print "test2"; chomp $_; @fields = (); @fields = split ('\|', $_); $flag = $fields[0]; if ( $flag eq inputdata ) -> comes from input file { print already $inputdata exists"\n"; $exitCode = 0; } else { # prints the next line of the sync edit results Add the data to the existing file $exitCode = 0; } } seek $data, 0, SEEK_SET; close( $f2 ); #close output file handle }
      o/p file
      james|chicago - first run alradeyd james exists - second run since input valeue is james and its + akready exit charles|chicago - third time run charles is not exists so written to t +he file

        Hello again leelu,

        The monks are happy to help you, but you need to meet us half-way. First, please follow wrog’s advice and format your post using paragraph and code tags:

        <p>First paragraph</p>
        <code>
        use strict; use warnings; ... close( $f2 ); #close output file handle }
        </code>
        <p>Final paragraph</p>

        See:

        Second, please follow afoken’s advice and post actual code. The code you have posted does not compile:

        • The $ sigil is missing from the first occurrence of $f1.
        • You have use strict but no declarations for the variables $results, @fields, $flag, and $inputdata.
        • The line print James|chicago "\n"; contains unquoted text; likewise the line print already $inputdata exists"\n";.

        Third, you say you are “getting error during while condition,” but don’t explain what that error is. Please see:

        and be sure to show the output you want to see, and exactly how it differs from the output you do see.

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Applying multiple file modes
by afoken (Chancellor) on May 25, 2015 at 06:50 UTC
    open( my $testfile, "+>> $file" ) $!\n"; if ( -z "$testfile" ) ---> checking for empty file

    This does not even compile. Post the actual code.

    Stringifying the file handle in $testfile will give you something like GLOB(0xb03cb8), NOT the filename. This test will always fail.

    To test if the file is empty, you do not need the -z test at all. You open the file for appending, so the file position is set to the end of the file. This is at position 0 only if the file is empty. tell will tell you the file position, or -1 on error. So, to repair that test:

    open my $testfile,'+>>',$file or die "Can't open '$file': $!"; my $pos=tell $testfile; ($pos!=-1) or die "tell failed for '$file': $!"; if ($pos==0) { # file is empty } else { # file has some content }

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Applying multiple file modes
by wrog (Friar) on May 25, 2015 at 01:42 UTC
    1. learn to use <p> and <code>
    2. get used to doing open ... or die (hint: open sometimes doesn't work)
    3. testing -z while you have the file open is likely to give a random result (you have no idea how long it's going to actually take to write the file; nothing is even remotely guaranteed until you close, and sometimes not even then; depending on your OS you may have to do a sync after closing. More likely you need to be doing Something Completely Different.)

    That's all I can tell right now until you format your code better.

Re: Applying multiple file modes
by thanos1983 (Parson) on May 25, 2015 at 09:18 UTC

    Helo leelu,

    It looks like already the monks have answered your question, but never the less I would like to add something. Read also about flock, it simply locks the file that you are trying to read/ write. In similar cases where you want to read and write on the same file it would be wise to lock it.

    In your case particularly maybe this is not necessary because you use only one process reading and writing, but in case that two or more processes (scripts) where accessing the same folder that there is possibility to produce conflicts.

    In order to use it you need to add only a few lines and it can save you from big big trouble.

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!