in reply to Re^2: How to handle the error when we try to read an empty text file in perl
in thread How to handle the error when we try to read an empty text file in perl
I mean if file is empty then it should add the $address in ppp.txt's first line otherwise go to the while loopin this case, rewording and expanding Discipulus' example:
you would have to move your code in like this (untested):my $file = 'C:\ppp.txt'; print "file '$file' "; if (-e $file) { print 'exists '; if (-z $file) { print 'but is empty'; } else { print 'and is not empty'; } } else { print q!doesn't exist!; } print "\n";
my $file = 'C:/ppp.txt'; print "file '$file' "; if (-e $file) { print 'exists '; if (-z $file) { # print 'but is empty'; my $fh, '>>', $file, or die "Could not open file '$file' for +appending: $!"; print$fh $address . ' or whatever you want'; close $fh; } else { # print 'and is not empty'; ... (your "old" open and while loop) } } else { # print q!doesn't exist!; ... (either create it or print error message) } print "\n";
|
|---|