status='replace' in a Fortran OPEN statement, when the file does not exist

Viewed 83

I wonder what the standard says when status='replace' is used in a open statement if the file DOES NOT exist. I read in different answers:

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.

So what is going on when file does not exist? Does it depend on the compiler?

1 Answers

Perhaps rather boringly, if the file does not exist it is simply created (Fortran 2018, 12.5.6.18):

If REPLACE is specified and the file does not already exist, the file is created and the status is changed to OLD.

This means, in particular, that it is not an error to "replace" a file which does not already exist. If you wish this to be an error, you could instead check whether the file exists before opening.

Related