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 <cid:part1.09000104.05070903@mts.net> for a better approach. The file is created with permissions of |0666| modified by the process' |umask| <cid:part2.09020904.00090408@mts.net> value.
If I read that right "+<" should be used. I changed the code to use "my $file = '+<mailstats.dat';" and ran it 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-boun...@listserv.activestate.com > [mailto:activeperl-boun...@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