Home > Archive > PERL Beginners > August 2006 > File locking issue
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]
| Author |
File locking issue
|
|
| Alok Nath 2006-08-31, 6:57 pm |
| Hi,
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.
=09
But it works correctly when I remove the file locking commands.
Please help ?? What I am doing wrong .
Regards,
Alok
=09
use XML::Twig;
use Fcntl qw(:DEFAULT :flock);
my $registerFile =3D "Register.xml" ;
my $twig =3D XML::Twig->new( pretty_print =3D> 'indented' );
# Save data into the xml file=20
my $node =3D XML::Twig::Elt->new(
'User', {'name' =3D> $usr},
XML::Twig::Elt->new( 'email' =3D>
$email )) ;
$node->paste( last_child =3D> $twig->root ) ;
=09
#use lock to write into the file
sysopen( FH, $registerFile, O_RDWR )
or die "can't open $registerFile: $!";
flock( FH, LOCK_EX)=20
or die "can't lock filename: $!";
truncate(FH, 0) or die "can't truncate filename: $!";
$twig->parsefile( $registerFile );
$twig->print_to_file( $registerFile ) ;=09
=09
close(FH)
| |
| John W. Krahn 2006-08-31, 6:57 pm |
| 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
| |
| Tom Phoenix 2006-08-31, 6:57 pm |
| On 8/31/06, Nath, Alok (STSD) <alok.nath@hp.com> wrote:
> What I am seeing is when I enable the file locking commands
> the file is completely wiped out.Just zero bytes.
> #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: $!";
> truncate(FH, 0) or die "can't truncate filename: $!";
I think I know where the "zero bytes" are coming from. :-)
> $twig->parsefile( $registerFile );
What are you hoping to parse from the file that you've just truncated
to zero bytes?
Hope this helps!
--Tom Phoenix
Stonehenge Perl Training
|
|
|
|
|