Code Comments
Programming Forum and web based access to our favorite programming groups.hi, I want to check the status of File handle before reading/writing to file ? How to do this ? like following open(FH_IN_FILE, ">file.txt"); # This statement is executed by some other function close(FH_IN_FILE); print FH_IN_FILE "SOME DATA"; here before writing to file, i want to check the status of FH_IN_FILE..(whether file is opened or closed ) Thanks __________________________________ Do you Yahoo!? Yahoo! Mail is new and improved - Check it out! http://promotions.yahoo.com/new_mail
Post Follow-up to this message> hi, > I want to check the status of File handle before > reading/writing to file ? How to do this ? > > like following > > open(FH_IN_FILE, ">file.txt"); > Which itself is a bad idea, always check that the open succeeded in the first place, open FH_IN_FILE, ">file.txt" or die "Can't open file for writing: $!"; > # This statement is executed by some other function > close(FH_IN_FILE); > Iffy, but ok. > > print FH_IN_FILE "SOME DATA"; > > here before writing to file, i want to check the > status of FH_IN_FILE..(whether file is opened or > closed ) > perldoc -f fileno Is about the only way to check (IIRC). The only way to know whether a file can really be written to is to try and catch any errors that occur. > Thanks > http://danconia.org
Post Follow-up to this message> open(FH_IN_FILE, ">file.txt");
>
> # This statement is executed by some other function
> close(FH_IN_FILE);
>
> print FH_IN_FILE "SOME DATA";
>
> here before writing to file, i want to check the
> status of FH_IN_FILE..(whether file is opened or
> closed )
You could do something like the following:
-----
#!/usr/local/bin/perl -w
use strict;
my %fhs;
# open the filehandle, store in hash
open $fhs{one}, '> test1.txt' or die "couldn't write file: $!\n";
# function takes hashref and name of which filehandle to use
sub blah {
my ($fh, $which) = @_;
close $fh->{$which};
# delete the filehandle when you close it
delete $fh->{$which};
}
blah(\%fhs, 'one');
# it's closed if it's gone from the hash
close $fhs{one} if exists $fhs{one};
-----
There might be a better way to do whatever you're trying to do,
though. What's the bigger problem you're trying to solve?
Dave
Post Follow-up to this messageMadhu Reddy wrote:
> hi,
Hello,
> I want to check the status of File handle before
> reading/writing to file ? How to do this ?
>
> like following
>
> open(FH_IN_FILE, ">file.txt");
>
> # This statement is executed by some other function
> close(FH_IN_FILE);
>
>
> print FH_IN_FILE "SOME DATA";
>
> here before writing to file, i want to check the
> status of FH_IN_FILE..(whether file is opened or
> closed )
if ( defined fileno FH_IN_FILE ) {
print FH_IN_FILE "SOME DATA";
}
else {
warn "Error: FH_IN_FILE is closed!\n";
}
John
--
use Perl;
program
fulfillment
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.