Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

With OPEN, unspecified STATUS behaves the same as STATUS = "replace"?

422 views
Skip to first unread message

Beliavsky

unread,
Mar 15, 2022, 9:53:18 AM3/15/22
to
I think the answer to the question in the topic is yes -- a Stack Overflow link is https://stackoverflow.com/questions/22635990/fortran-95-open-statement-status-variable-unknown-vs-replace . So I plan to tweet the following:

open(unit=10,file="foo.txt",action="write",status="replace")

overwrites an existing file or creates a new file. Compilers usually treat
an unspecified STATUS the same way, but this is not mandated. STATUS="new" prevents overwriting a file.

Code (also at https://github.com/Beliavsky/FortranTip/blob/main/open_file.f90):

program open_file
implicit none
character (len=*), parameter :: fname = "temp.txt"
integer, parameter :: iu = 10
integer :: ierr
open (unit=iu,file=fname,action="write")
! compilers will typically overwrite temp.txt if it exists,
! but this is not mandated with unspecified STATUS
write (iu,*) "a"
close (iu)
open (unit=iu,file=fname,action="write",status="replace")
write (iu,*) "b"
close (iu)
open (unit=iu,file=fname,action="write",status="new",iostat=ierr)
if (ierr == 0) then
write (iu,*) "c"
close (iu)
else
write (*,*) "file " // fname // " already connected"
end if
open (unit=iu,file=fname,action="write",status="new")
write (iu,*) "c"
end program open_file
! gfortran output (ifort, g95, flang similar):
! file temp.txt already connected
! At line 21 of file open_file.f90 (unit = 10)
! Fortran runtime error: Cannot open file 'temp.txt': File exists

Arjen Markus

unread,
Mar 15, 2022, 10:14:56 AM3/15/22
to
On Tuesday, March 15, 2022 at 2:53:18 PM UTC+1, Beliavsky wrote:
> I think the answer to the question in the topic is yes -- a Stack Overflow link is https://stackoverflow.com/questions/22635990/fortran-95-open-statement-status-variable-unknown-vs-replace . So I plan to tweet the following:
>

That does not seem quite right: it presumes that you are going to write to the file, whereas you can open an existing file that way if you merely want to read it.

Regards,

Arjen

Beliavsky

unread,
Mar 15, 2022, 10:21:10 AM3/15/22
to
I meant for the question to apply to the case of action="write" but did not want to make the subject line too long. For the case of action="read" I would use status="old", and either leave out the iostat and let the program terminate if the file does not exist or set and handle iostat.

Thomas Koenig

unread,
Mar 15, 2022, 12:54:20 PM3/15/22
to
Beliavsky <beli...@aol.com> schrieb:
> I think the answer to the question in the topic is yes --

Not generally.

Looking at the standard:

12.5.6.18 STATUS= specifier in the OPEN statement

[...]

If UNKNOWN is specified, the status is processor dependent. If
this specifier is omitted, the default value is UNKNOWN.

Seems like gfortran actually does not document exactly what
it does (it does for ACTION). Something to be cleared up
in the documentation.

Steve Lionel

unread,
Mar 16, 2022, 5:49:35 PM3/16/22
to
On 3/15/2022 9:53 AM, Beliavsky wrote:
> I think the answer to the question in the topic is yes -- a Stack Overflow link ishttps://stackoverflow.com/questions/22635990/fortran-95-open-statement-status-variable-unknown-vs-replace . So I plan to tweet the following:
>
> open(unit=10,file="foo.txt",action="write",status="replace")
>
> overwrites an existing file or creates a new file. Compilers usually treat
> an unspecified STATUS the same way, but this is not mandated. STATUS="new" prevents overwriting a file.

To answer the question in the subject, I would say "Not necessarily".
The standard says that if STATUS is not specified, the value used is
"UNKNOWN". In the implementations I am familiar with, this value does
behave the same as "REPLACE", but I'd be hesitant about generalizing that.

Writing a test case to determine behavior for a given implementation is
one thing, but then saying this is the behavior for all implementations
is unwarranted.

--
Steve Lionel
ISO/IEC JTC1/SC22/WG5 (Fortran) Convenor
Retired Intel Fortran developer/support
Email: firstname at firstnamelastname dot com
Twitter: @DoctorFortran
LinkedIn: https://www.linkedin.com/in/stevelionel
Blog: https://stevelionel.com/drfortran
WG5: https://wg5-fortran.org

gah4

unread,
Mar 16, 2022, 5:54:31 PM3/16/22
to
On Wednesday, March 16, 2022 at 2:49:35 PM UTC-7, Steve Lionel wrote:

(snip)

> To answer the question in the subject, I would say "Not necessarily".
> The standard says that if STATUS is not specified, the value used is
> "UNKNOWN". In the implementations I am familiar with, this value does
> behave the same as "REPLACE", but I'd be hesitant about generalizing that.

> Writing a test case to determine behavior for a given implementation is
> one thing, but then saying this is the behavior for all implementations
> is unwarranted.

But the important thing, is that it is true in some cases.

That is, you could accidentally delete your "once in a lifetime" file
if you didn't think about this in advance. Of course it is good to
put read-only protection on those files, and presumably it
won't override that.



Ron Shepard

unread,
Mar 17, 2022, 3:03:22 AM3/17/22
to
On 3/16/22 4:49 PM, Steve Lionel wrote:
[...]
> To answer the question in the subject, I would say "Not necessarily".
> The standard says that if STATUS is not specified, the value used is
> "UNKNOWN". In the implementations I am familiar with, this value does
> behave the same as "REPLACE", but I'd be hesitant about generalizing that.
>
> Writing a test case to determine behavior for a given implementation is
> one thing, but then saying this is the behavior for all implementations
> is unwarranted.

I think this might depend on more than just the compiler, it might also
depend on the file system characteristics. An OS can support many file
systems these days, including network file systems hosted by a different
remote OS.

For another example, the VAX/VMS system had a file naming convention
that included version numbers, xxx.yyy.n. If you opened a file with
'UNKNOWN', then you opened the file with the highest version number if
it existed, or you opened the xxx.yyy.1 file if it didn't. If you opened
the file with 'NEW', then you would get a new file with an incremented
version number, and the old files were still there. This was before
'REPLACE' was one of the options. I think before the VAX, the DECSYSTEM
had a similar convention, but those OPEN() statements had a slightly
different set of keywords because that was before f77 standardized that
behavior.

$.02 -Ron Shepard


gah4

unread,
Mar 17, 2022, 3:41:45 AM3/17/22
to
On Thursday, March 17, 2022 at 12:03:22 AM UTC-7, Ron Shepard wrote:

(snip)

> I think this might depend on more than just the compiler, it might also
> depend on the file system characteristics. An OS can support many file
> systems these days, including network file systems hosted by a different
> remote OS.

> For another example, the VAX/VMS system had a file naming convention
> that included version numbers, xxx.yyy.n.

It still does. Well, VAX/VMS is pretty much unsupported now, but
Alpha/VMS and IA64/VMS still exist and are still supported.

DECnet has support for remote files based on DEC file systems.
I am not sure about using NFS to/from VMS based systems.

> If you opened a file with
> 'UNKNOWN', then you opened the file with the highest version number if
> it existed, or you opened the xxx.yyy.1 file if it didn't. If you opened
> the file with 'NEW', then you would get a new file with an incremented
> version number, and the old files were still there. This was before
> 'REPLACE' was one of the options. I think before the VAX, the DECSYSTEM
> had a similar convention, but those OPEN() statements had a slightly
> different set of keywords because that was before f77 standardized that
> behavior.

It is supposed to work such that if you open without a ;(the version qualifier)
for reading you get the most recent, and for writing you get a new version.
Exactly how that works with Fortran, I am not so sure.

I always tried to have my Fortran programs that read files open them
in a read only mode, to avoid confusion by the OS, and accidentally
deleting one.

Before Fortran 77, the rules were somewhat complicated by the
system figuring out what you meant, with the first I/O statement
being READ or WRITE.

Ron Shepard

unread,
Mar 18, 2022, 2:54:13 AM3/18/22
to
On 3/17/22 2:41 AM, gah4 wrote:
> It is supposed to work such that if you open without a ;(the version qualifier)
> for reading you get the most recent, and for writing you get a new version.
> Exactly how that works with Fortran, I am not so sure.

Ah yes, the version number was delimited with a ; not a period as I said
in my post. It has been a while since I used a vax.

I also remember something like a directory= keyword in open statements,
that allowed you to open files in different directories than the default.

And I also remember something like carriagecontrol='list', because the
default was to use ASA formatting, which you almost never really wanted.

Those things didn't make it into the standard, probably because they
were all too specific to the DEC operating systems and file systems.

$.02 -Ron Shepard
0 new messages