Forum Moderators: coopster & phranque

Message Too Old, No Replies

About this Code

About this Code

         

lindajames

8:40 am on May 8, 2003 (gmt 0)

10+ Year Member



Hi,

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;

ShawnR

1:21 pm on May 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



With the line

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.

lindajames

2:08 pm on May 8, 2003 (gmt 0)

10+ Year Member



So, how shall i do it? how should the code look?

ShawnR

10:43 pm on May 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, what are you trying to do? Why do you create the empty file?

To test if the file is not zero size, instead of:

if(-e $file) {

try

if((-e $file) and not(-z $file)) {

Shawn