Re: File locking issue
- From: krahnj@xxxxxxxxx (John W. Krahn)
- Date: Thu, 31 Aug 2006 09:12:22 -0700
Nath, Alok (STSD) wrote:
Hi,
Hello,
I am trying to save data in an xml file.
What I am seeing is when I enable the file locking commands
the file is completely wiped out.Just zero bytes.
But it works correctly when I remove the file locking commands.
Please help ?? What I am doing wrong .
Regards,
Alok
use XML::Twig;
use Fcntl qw(:DEFAULT :flock);
my $registerFile = "Register.xml" ;
my $twig = XML::Twig->new( pretty_print => 'indented' );
# Save data into the xml file
my $node = XML::Twig::Elt->new(
'User', {'name' => $usr},
XML::Twig::Elt->new( 'email' =>
$email )) ;
$node->paste( last_child => $twig->root ) ;
#use lock to write into the file
sysopen( FH, $registerFile, O_RDWR )
or die "can't open $registerFile: $!";
flock( FH, LOCK_EX)
or die "can't lock filename: $!";
flock() locks the file, it is NOT changing the size.
truncate(FH, 0) or die "can't truncate filename: $!";
truncate() changes the size of the file, here you are changing the size to
zero bytes!
$twig->parsefile( $registerFile );
$twig->print_to_file( $registerFile ) ;
close(FH)
John
--
use Perl;
program
fulfillment
.
- References:
- File locking issue
- From: Alok Nath
- File locking issue
- Prev by Date: Re: Totally lost - need a starting point
- Next by Date: DBI::st=HASH(0x88f6520)
- Previous by thread: File locking issue
- Next by thread: Re: File locking issue
- Index(es):
Relevant Pages
|