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

    From Beliavsky@21:1/5 to All on Tue Mar 15 06:53:15 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)