Help with file stuff

Help with file stuff

am 17.07.2009 21:01:33 von Barry Brevik

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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Help with file stuff

am 17.07.2009 21:12:23 von Wayne Simmons

I'm guessing seek won't go past the end of the file, you'll have to add
logic to add "empty" records to your file in order to go that far. It is
unclear from the Perl documentation on seek (as well as "man fseek") that
this is the behavior, but it seems rational and the most likely reason for
what you're experiencing.

Psudeo code:
Is seek position past EOF?
For each record past EOF
Add blank record to EOF
Seek position
Write new record

-Wayne Simmons

--
Software Engineer
InterSystems USA, Inc.
303-858-1000


-----Original Message-----
From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of Barry
Brevik
Sent: Friday, July 17, 2009 1:02 PM
To: activeperl@listserv.ActiveState.com
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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Help with file stuff

am 17.07.2009 21:38:02 von Ken Cornetet

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: activeperl-bounces@listserv.ActiveState.com [mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of Barry Brevik
Sent: Friday, July 17, 2009 3:02 PM
To: activeperl@listserv.ActiveState.com
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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Help with file stuff

am 28.07.2009 07:23:22 von basil.daoust

from the activeperl manual that I have...

You can put a |'+'| in front of the |'>'| or |'<'| to indicate that you
want both read and write access to the file; thus |'+<'| is almost
always preferred for read/write updates--the |< '+|' >> mode would
clobber the file first. You can't usually use either read-write mode for
updating textfiles, since they have variable length records. See the
*-i* switch in the perlrun manpage
for a better approach. The file is created with permissions of |0666|
modified by the process' |umask|
value.

If I read that right "+<" should be used.

I changed the code to use "my $file = '+ with a seek of 128, then 30, 1, and 60 and got the following.
This is a test record. This is a test record. This is a
test record. This is a test
record.

When I tried with +> the previous write was lost, but the seek worked.
good luck.

Ken Cornetet wrote:
> 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: activeperl-bounces@listserv.ActiveState.com [mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of Barry Brevik
> Sent: Friday, July 17, 2009 3:02 PM
> To: activeperl@listserv.ActiveState.com
> 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
> ActivePerl@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> _______________________________________________
> ActivePerl mailing list
> ActivePerl@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
> __________ Information from ESET Smart Security, version of virus signature database 4263 (20090721) __________
>
> The message was checked by ESET Smart Security.
>
> http://www.eset.com
>
>
>
>
>
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs