From: helvio on
Hi all!

I have a simple question. I want to check if a given logical unit is
connected to an opened file. As a test, I wrote the following short
program:

program foo

logical :: lfile

inquire (unit=42, exist=lfile)
write(*,*) lfile
open (unit=42, file='file.dat')
inquire (unit=42, exist=lfile)
write(*,*) lfile
close(unit=42)
inquire (unit=42, exist=lfile)
write(*,*) lfile

end program foo

I compiled it with gfortran and g95 from Cygwin, and mpif90 and
mpiexec from MPICH2. But the executable always outputs the following:

T
T
T

I expected the executable to return:

F
T
F

Obviously I am doing something wrong, or misunderstanding the usage of
INQUIRE! Could you please tell me how should I change my program in
order to obtain the latter output (to return FALSE whenever a logical
unit is available, i.e. not connected to an opened file)?

Cheers,
-- helvio
From: Richard Maine on
helvio <helvio.vairinhos(a)googlemail.com> wrote:

> I have a simple question. I want to check if a given logical unit is
> connected to an opened file.

Just as a terminology nit, note that "connected to an open file" is
redundant. "Connected" and "opened" are synonyms; if a unit is connected
to a file, then that means the file and unit are both opened.
....
> inquire (unit=42, exist=lfile)

> Could you please tell me how should I change my program in
> order to obtain the latter output (to return FALSE whenever a logical
> unit is available, i.e. not connected to an opened file)?

Right general idea; just the wrong keyword.

When you are inquiring by unit (as you are), "exist" just tells whether
a unit of that number exists. It doesn't have much to do with whether
the unit is connected to anything. I don't tend to find "exist" very
useful for inquire by unit. It is more for things like to figure out
that you can't use unit numbers higher than 99 (as used to be a common
limit). It basically tells you whether a given unit number is one that
tha compiler supports at all (which doesn't tend to be as much of a
problem as it once was).

What you want is the "opened" keyword. Substitute it in place of your
"exist" everywhere and I think you'll find it does what you want.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: helvio on
On May 27, 6:07 pm, nos...(a)see.signature (Richard Maine) wrote:
> What you want is the "opened" keyword. Substitute it in place of your
> "exist" everywhere and I think you'll find it does what you want.

Thank you, Richard! :)