I have the following code that reads data.dat and if the file doesnt exist it creates it and displays a message saying "There is not data in the file!" however, the problem is that when i run the script the second time and if the data.dat file is empty it doesnt display the message: "There is not data in the file!" can someone tell me how i can make that message display only if the data.dat file is empty.
Any Help Would be much appreciated
Cheers
Linda
my $file = "data.dat";
print "Content-Type: text/html\n\n";
if(-e $file) {
open(FILE, $file);
while(<FILE> ) {
chomp;
print "<a href=\"/cgi-bin/edit.cgi\">$_</a><BR>\n";
}
close(FILE);
} else {
open(FILE, ">$file");
close(FILE);
print "There is not data in the file!";
}
exit;
open(FILE, ">$file");
you are creating the file.
So the second time around the file will exist, so (-e $file) will return true. You could check the size of the file (-s $file) or check it is zero size (-z $file), but it would be better to check that the data in the file bears some semblance to what you expect.