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.
>
> 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.
>
> 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
I think the problem is that Fortran can't know what your intentions
are. It probably thinks you want to create a _new_ file. (You aren't
specifying STATUS="OLD" or anything).
So, Fortran creates the new file, gives you a handle to it, and
cheerfully reports back it had no problem - hence, iostat = 0. If
you think about it, if opening a non-existing file were an error,
how could you create new files?
Use inquire() if you want to know whether a file exists; then branch
accordingly.
Alvaro Fernandez
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|