I think your oddball way of opening the file may be a problem (use strict
complains about it). Also, you want ">" not ">>" since that says to always
append your writes to the end of the file.
You may want binmode as well.
This appears to work
use strict;
use warnings;
my $file = '+>mailstats.dat';
if (open MAILSTATS, $file) {
if (seek MAILSTATS, 128, 0) {
print "Current file position is ", tell(MAILSTATS), "\n";
print MAILSTATS "This is a test record.";
} else {
print " Can not seek to location 128.\n";
}
close MAILSTATS;
}
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Barry Brevik
Sent: Friday, July 17, 2009 3:02 PM
To: [email protected]
Subject: Help with file stuff
I'm creating a file which is just an ASCII text file, but I'm writing
fixed length records into it. At any time, I need to reopen the file and
write a record to a byte position calculated at run time. The write
position will frequently be beyond the end of the current file length,
so I need for the file to be extended.
In the test code below, I have replaced the calculated position with
"128" for convenience. When I run the code, it creates the file, and it
says that the current position is "128", but when I write the record, it
always ends up at the very beginning of the file.
Any clue as to what I am doing wrong?
------------------------------------------
use warnings;
$MAILSTATS = '+>>mailstats.dat';
if (open MAILSTATS)
{
if (seek MAILSTATS, 128, 0)
{
print "Current file position is ", tell(MAILSTATS), "\n";
print MAILSTATS "This is a test record.";
}
else
{
print " Can not seek to location 128.\n";
}
close MAILSTATS;
}
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs