From: Allamarein on
Dummy question: is there a command to know the status of the file is
close or open?
I would to provide number file and know if it is close or open.
Eg:
Query_status(20)
From: dpb on
Allamarein wrote:
> Dummy question: is there a command to know the status of the file is
> close or open?
> I would to provide number file and know if it is close or open.
> Eg:
> Query_status(20)

logical :: openstat

....

inquire(20, opened=openstat)

if(openstat) then
...
else
...
endif

--
From: Richard Maine on
Allamarein <matteo.diplomacy(a)gmail.com> wrote:

> Dummy question: is there a command to know the status of the file is
> close or open?
> I would to provide number file and know if it is close or open.
> Eg:
> Query_status(20)

See the INQUIRE statement. It can inquire about all kinds of I/O-related
things, including that one. I think it more useful in this case, to
direct you to look up the statement in reference materials than to just
show you the code. Knowing that the name of the statement you want is
INQUIRE should be suficient direction.

As an aside, a file and a unit number are not the same thing. One can be
connected to another (that's what opening does), but they aren't the
same. It appears that you are asking about how to determine whether a
unit number is open - not whether a file is open. Both options exist.

(I was slightly surprised, when I checked before writing this answer, to
note that there is an option to query about whether a file is open and
that it is a logical variable so that 'UNKNOWN' is not an option. I
didn't think of that as the kind of information the standard required to
compiler to be able to track. That the compiler run-times could tell
whether or not a unit number was open seems reasonably "obvious" to me.
That they could necessarily tell whether a file was open, for all
concievable operating systems and circumstances, seems far less obvious.
Hmm. I wonder whether a compiler might be able to return an error status
for a query that it could not answer? I suppose probably so, the set of
error conditions being unspecified by the standard.)

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: Paul van Delst on
Allamarein wrote:
> Dummy question: is there a command to know the status of the file is
> close or open?
> I would to provide number file and know if it is close or open.
> Eg:
> Query_status(20)

You can use the INQUIRE statement as others have indicated. I use something like the following module:

MODULE File_Utility

IMPLICIT NONE
PRIVATE
PUBLIC :: File_Open
INTERFACE File_Open
MODULE PROCEDURE File_Open_by_Unit
MODULE PROCEDURE File_Open_by_Name
END INTERFACE File_Open

CONTAINS

FUNCTION File_Open_by_Unit( FileID ) RESULT ( Is_Open )
INTEGER, INTENT( IN ) :: FileID
LOGICAL :: Is_Open
INQUIRE( UNIT = FileID, OPENED = Is_Open )
END FUNCTION File_Open_by_Unit

FUNCTION File_Open_by_Name( Filename ) RESULT ( Is_Open )
CHARACTER( * ), INTENT( IN ) :: Filename
LOGICAL :: Is_Open
INQUIRE( FILE = Filename, OPENED = Is_Open )
END FUNCTION File_Open_by_Name

END MODULE File_Utility


so that I can query if a file is open by unit number or filename via the same interface, e.g.


USE File_Utility

IF ( File_Open(20) ) THEN
...
END IF

IF ( File_Open('my_file.dat') ) THEN
...
END IF


I've found it quite handy.

cheers,

paulv
From: robert.corbett on
On Jul 29, 10:51 am, nos...(a)see.signature (Richard Maine) wrote:

> (I was slightly surprised, when I checked before writing this answer, to
> note that there is an option to query about whether a file is open and
> that it is a logical variable so that 'UNKNOWN' is not an option. I
> didn't think of that as the kind of information the standard required to
> compiler to be able to track. That the compiler run-times could tell
> whether or not a unit number was open seems reasonably "obvious" to me.
> That they could necessarily tell whether a file was open, for all
> concievable operating systems and circumstances, seems far less obvious.
> Hmm. I wonder whether a compiler might be able to return an error status
> for a query that it could not answer? I suppose probably so, the set of
> error conditions being unspecified by the standard.)

You are correct that there are operating systems where it is
difficult,
if not impossible, to tell if a file is open. The problem for a
Fortran
run-time input/output system is a little different. For the INQUIRE
statement, the question is not if the file is open, but if the file is
connected to a Fortran external unit. The implementation of INQUIRE
by
file in the Fortran run-time systems I have supported for the past 30
years is to scan the unit table looking for a unit connected to the
file.
The Fortran run-time systems for UNIX and UNIX-like operating systems
have assumed that the devno and inode numbers taken together uniquely
identify a file. On other operating systems, the file name has been
assumed to uniquely identify the file.

Note that a file might be connected to more than one unit. For
example,
on UNIX and UNIX-like operating systems, device files such as
"/dev/console" and "/dev/tty" might have the same devno and inode
numbers. Those files might simultaneously be connected to the
standard
input unit, the standard output unit and the standard error unit. Of
course, in such a case, the program is not standard conforming and
therefore its behavior is undefined.

Robert Corbett