Home > Archive > PERL Beginners > August 2005 > edit / seek woes
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Brent Clark 2005-08-29, 9:55 pm |
| Hi List
This whole, using s has kind put me off for the day.
Would someone be so kind as to please over look me code and if possible, point out where I am going wrong.
Kind Regards
Brent Clark
========================================
=======================
open(HDATFILE, "+< $fileName.dat") or die "Cant open file $fileName.dat $!";
flock (HDATFILE, LOCK_EX) or die "Cant lock file $fileName.dat $!";
my $writePos = 0;
while (<HDATFILE> ){
my $readPos = tell HDATFILE;
if(m/^p11[016]/io){
$found110 = 'y' unless m/^p110/i;
$found111 = 'y' unless m/^p111/i;
$found116 = 'y' unless m/^p116/i;
s HDATFILE, $writePos, 0;
print HDATFILE &compareRmNames($fileName, $_);
$writePos = tell HDATFILE;
s HDATFILE, $readPos, 0;
}else{
s HDATFILE, $writePos, 0;
print HDATFILE $_;
$writePos = tell HDATFILE;
s HDATFILE, $readPos, 0;
}
}
if ($found110 eq 'y'){
print "Inserting missing p110 code : $fileName <br />\n";
print "#" x 100, "<br />\n";
print HDATFILE &insertMissingPCode($fileName, 'p110');
}
if ($found111 eq 'y'){
print "Inserting missing p111 code : $fileName <br />\n";
print "#" x 100, "<br />\n";
print HDATFILE &insertMissingPCode($fileName, 'p111');
}
if ($found116 eq 'y'){
print "Inserting missing p116 code : $fileName <br />\n";
print "#" x 100, "<br />\n";
print HDATFILE &insertMissingPCode($fileName, 'p116');
}
truncate(HDATFILE, $writePos);
flock (HDATFILE, LOCK_UN) or die "Cant unlock file $fileName.dat $!";
close (HDATFILE) or die "Cant close file $fileName.dat $!";
| |
| Jeff 'japhy' Pinyan 2005-08-29, 9:55 pm |
| On Aug 29, Brent Clark said:
> Would someone be so kind as to please over look me code and if possible, point
> out where I am going wrong.
Would you be so kind as to explain what happens with it that you didn't
expect to happen? (Or what doesn't happen that you'd expected to happen.)
> open(HDATFILE, "+< $fileName.dat") or die "Cant open file $fileName.dat $!";
> flock (HDATFILE, LOCK_EX) or die "Cant lock file $fileName.dat $!";
Make sure you're including the Fcntl module for the LOCK_EX constant.
> my $writePos = 0;
> while (<HDATFILE> ){
> my $readPos = tell HDATFILE;
>
> if(m/^p11[016]/io){
>
> $found110 = 'y' unless m/^p110/i;
> $found111 = 'y' unless m/^p111/i;
> $found116 = 'y' unless m/^p116/i;
>
> s HDATFILE, $writePos, 0;
> print HDATFILE &compareRmNames($fileName, $_);
> $writePos = tell HDATFILE;
> s HDATFILE, $readPos, 0;
Unless you are writing the EXACT SAME NUMBER OF BYTES to the file, you are
going to either over-write or under-write content.
You're probably better off using a temporary file, or Perl's "in-place"
editing.
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
|
|
|
|
|