Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

How to check the FILE HANDLE STATUS ???
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

Report this thread to moderator Post Follow-up to this message
Old Post
Madhu Reddy
09-24-04 08:59 PM


Re: How to check the FILE HANDLE STATUS ???
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Wiggins d Anconia
09-24-04 08:59 PM


Re: How to check the FILE HANDLE STATUS ???
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Dave Gray
09-25-04 01:56 AM


Re: How to check the FILE HANDLE STATUS ???
Madhu 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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
09-25-04 01:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:25 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.