Rajesh Vattem wrote:
Hi,
I am trying to read and write to a file with a single file handle operation.
Could you please suggest a change in the below script so that it will change
one line of a file based on the pattern compared with (Presently, the script
doesn't work properly because I don't delete the already existing contents)
my $infile;
my $AutoCampPath = "SELFile=C:\\Raj\\camp.sel";
open ($infile, "+<CTS_MMI.ini") or die "couldn't open the specified file:
$!";
my @array = <$infile>;
for(my $i = 0; $i < @array; $i++)
{
$array[$i] = $AutoCampPath if($array[$i] =~ /SELFile/);
}
print $infile @array;
close($infile);
I agree with what Bill said about doing it with two file handles. but if
you accept the risks of doing it this way, I think all you're missing is
a seek() to return your file position back to the start of the file.
Your initial slurp leaves your file pointer at EOF. That's why when you
print your modified content back to the file, you get it copied onto the
end of the existing file.
You could try something like this. It's the same thing but perhaps a bit
more perlish...
my $AutoCampPath = "SELFile=C:\\Raj\\camp.sel";
open INFILE, "+<CTS_MMI.ini" or die "opening file: $!";
my @lines = <INFILE>;
seek INFILE,0,0;
for(@lines){
s/^SELFile.*$/$AutoCampPath/;
print INFILE;
}
close INFILE;
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs