Dear Bertrand:
Bertrand Meltz wrote:
> I thought that by having the IOSTAT=error_condition,
> I would detect whether I'm opening a file that does not exist.
>
> Apparently I was wrong.
Yes, you were wrong. See below.
>
>
> In my code, I have :
>
> open( unit=...., ..., IOSTAT=error_condition)
> if ( error_condition == 0 )
> ! read the file
> ...
> end if
>
> it appears that even though the file does not exist, I get
> error_condition = 0,
> and the my reading of the files fails.
Yes, this is perfectly standard-conforming. In fact, this is how most
compilers behave.
>
>
> I have the same behaviour with 3 different compilers ( IBM, SUN and
> Compaq/TRU64).
> I searched the F95 standard, but it does not say explicitly (or not
> explicitly enough for me) whether opening a non-existing file should
> be an error.
> Does anybody ahve the final word about that?
>
> B. Meltz
>
You need to look in BOTH section 9.3.4.2 of the Fortran 95 AND the
reference manual of your favorite compiler.
Section 9.3.4.2 covers the STATUS= specifier. Here is some of the text.
[Begin section 9.3.4.2]
9.3.4.2 STATUS= specifier in the OPEN statement
The scalar-default-char-expr shall evaluate to OLD, NEW, SCRATCH, REPLACE, or
UNKNOWN. If OLD is specified, the file shall exist. If NEW is specified, the
file shall not exist.
Successful execution of an OPEN statement with NEW specified creates the file
and changes the status to OLD. If REPLACE is specified and the file does not
already exist, the file is created and the status is changed to OLD. If
REPLACE is specified and the file does exist, the file is deleted, a new file
is created with the same name, and the status is changed to OLD.
. . .
If UNKNOWN is specified, the status is processor dependent. If this specifier
is omitted, the default value is UNKNOWN.
[End of section 9.3.4.2]
If the STATUS= specifier does not appear, the value of STATUS is UNKNOWN
and the compiler can open the file any way that it likes. In almost every
case, if the STATUS is UNKNOWN, real Fortran compilers check to see that the
file exists. If it does exist, it opens the file with STATUS=OLD. If it does
not exist, it opens the file with STATUS=NEW.
This behavior is not required by the Fortran standard, but it is very much
a de-facto "standard" enforced by the laws of the marketplace.
--
----------
Sincerely,
Craig T. Dedo Internet:
[log in to unmask]
Elmbrook Computer Services Voice Phone: (262) 783-5869
17130 W. Burleigh Place Fax Phone: (262) 783-5928
Brookfield, WI 53005-2759 Disclaimer: These opinions
are mine alone.
USA They do NOT
represent any organization.
"They that can give up essential liberty to obtain a little temporary
safety deserve neither liberty nor safety." -- Benjamin Franklin (1759)
|