Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<<pre>my $map_file="/home/rpsa/DEMO/mapfile.txt";
open( my $fh,  '<', $map_file )            or die( $! );
open( my $fh1, '>', "result_file.txt" ) or die( $! );

while ( my $line = <$fh> ) {
    next unless ( defined( $line ) && $line );
    my @cols = split( /:/, $line );
    print $fh1 $cols[-1], "\n";
}

if(open( my $fh2, '<', "result_file.txt" ) || die( $! ))
{
while ( my $row = <$fh2> ) {
	chomp $row;
	print "****$row****";
}
}
else
{
warn "could not open the file ";
}


What I have tried:

I had tried to read the text file from the first while loop but am unable to do it.Help me to solve my issues.
Posted
Updated 21-Feb-17 1:20am

1 solution

That is because the file result_file.txt is still opened for writing and the content has not yet been written physically (flushed) when you are opening it again for reading.

While you can force flushing using $fh1->flush(), a better solution is closing file handles when not needed anymore:

PERL
close($fh1);
close($fh);
if(open( my $fh2, '<', "result_file.txt" ) || die( $! ))
{
    while ( my $row = <$fh2> ) {
    	chomp $row;
    	print "****$row****";
    }
    close($fh2);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900