in reply to Re: Replacing a string in a file
in thread Replacing a string in a file

For opening the file for reading writing, how do you go about doing this? Do you have sample code? It is for a config file which is very small, so this solution would be great.

Thanks,
Avi

Replies are listed 'Best First'.
Re: Replacing a string in a file
by perlplexer (Hermit) on Jul 16, 2003 at 21:28 UTC
    Here's how I would approach this (not tested)
    use strict; use Fcntl ':flock'; my $iniFile = 'config.ini'; if (open my $ini, "+<$iniFile"){ if (flock $ini, LOCK_EX){ my @data = <$ini>; # Work with @data -- delete lines, update lines, etc. if (truncate($ini, 0) and seek($ini, 0, 0)){ print "Can't write to $iniFile : $!\n" unless print $ini @data; }else{ print "Can't truncate/seek : $!\n"; } }else{ print "Can't lock : $!\n"; } close $ini; }else{ print "Can't open $iniFile : $!\n"; }
    --perlplexer