in reply to Applying multiple file modes

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,

Replies are listed 'Best First'.
Re^2: Applying multiple file modes
by leelu (Initiate) on May 25, 2015 at 17:28 UTC
    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,