Thread: Writing to file
View Single Post
Old Jun 7th, 2005, 9:45 AM   #2
sykkn
Hobbyist Programmer
 
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5 sykkn is on a distinguished road
you are on the right track. Instead of generating the file name timestamp while opening the file ... create and store it to be used for the check you will do in you while loop ... then before writing ... generate another copy of the timestamp using the current time and compare the two ... if they differ, close the current log file and reopen with the new filename.

# I truncated a bit ...

my @t = localtime(time) ;
my $fileTimeStamp = ($t[5]+1900) . "-" . ($t[4]+1) . "-" . $t[3];
open(OUTPUT_FILE, ">>/data2/elsonba/zizo/" . $fileTimeStamp . ".log");

select OUTPUT_FILE; $| = 1;

my $line;

while (<SOCKET>)
{
     my @ct = localtime(time()); # current localtime
     my $currentTimeStamp = ($t[5]+1900) . "-" . ($t[4]+1) . "-" . $t[3];
     if($fileTimeStamp ne $currentTimeStamp)
     {
           $fileTimeStamp = $currentTimeStamp; # set file stamp to current for use in open and later checks
           close OUTPUT_FILE || warn($!);
           open(OUTPUT_FILE,">>/data2/elsonba/zizo/" . $fileTimeStamp . ".log") || die($!);
     }

     print OUTPUT_FILE "$_\n";
}
close SOCKET or die "close: $!";
close OUTPUT_FILE;

that's untested ... it should point you in the right direction.
sykkn is offline   Reply With Quote