Usage of newunit for unformatted files


I have implemented a code, where i open a file in the ‘constructor’
of an extended type as unformatted file.

When I try to write to this files in another method, both gfortran (11.2 Linux X64) and
aocc 3.1.0 (based on the “traditional” flang ) cause runtime error, whereas ifort 2021.4.0
runs successful, i.e., the unformatted file is written.

A minimal testcase is

module mod_testfile
  use iso_fortran_env
  implicit none

  type testfile
     integer                             :: unit
       procedure, pass(self)             :: write_and_close
  end type testfile
  interface testfile
     module procedure testfile_open
  end interface testfile
  type(testfile) function testfile_open(filename) result(self)
    implicit none
    character(len=*), intent(in) :: filename
    open (newunit = self % unit, file=filename,  form='unformatted')
    print*, 'output_unit :', self % unit
  end function testfile_open

  subroutine write_and_close(self, data)
    implicit none
    class(testfile), intent(out)               :: self
    integer                                    :: data
    write(self % unit) data
    close(self % unit)    
  end subroutine write_and_close
end module mod_testfile

program file_test  
  use mod_testfile
  implicit none
  type(testfile)                             :: my_test
  my_test =  testfile('test.bin')
  call my_test % write_and_close(42 )
end program file_test

Output Gfortran 11.2:
output_unit : -10
At line 28 of file write_minimal_file.f90
Fortran runtime error: Missing format for FORMATTED data transfer

Error termination. Backtrace:
#0 0x7f9bf9b34d5a
#1 0x7f9bf9b35869
#2 0x7f9bf9b3654f
#3 0x7f9bf9d7d1a0
#4 0x4012d9
#5 0x4014b0
#6 0x4014e7
#7 0x7f9bf97930b2
#8 0x4010ed

output aocc-3.1.0:

output_unit : -13
FIO-F-215/unformatted write/unit=0/formatted/unformatted file conflict.
File name = 'stderr ', formatted, sequential access record = 0
In source file write_minimal_file.f90, at line number 28

output Ifort:
output_unit : -129

The problem disappears when the logic for opening and writing the file
are put in the same subroutine.

I am not sure, whether my code causes undefined behavior, or whether the problem
is related to the compilers.

In the latter case I will a bug report to the gfortran mailing list.

Maybe further testcases are required to isolate the problem (e.g.-, non-OO code).


At line 28 of file write_minimal_file.f90
Fortran runtime error: Missing format for FORMATTED data transfer

Thus, replace line 28 with

write(self % unit, *) data
1 Like

That won’t do: the file is opened with “form = ‘unformatted’”. No, the actual problem is that the argument self is defined as intent(out). This means that anything in the derived type should be considered undefined, unless explicitly set in the routine. Apparently, Intel Fortran keeps the values whereas gfortran and aocc may have different ideas. The consequence is that self%unit is no longer the value returned via NEWUNIT=, but rather an arbitrary unit number and then the file that will be connected (by default) is formatted.


Hallo Arjen,

removing intent(out) led to the expected behavoior in each of the
three compilers.

Thank you very much.


YW - I happened to spot the problem and was too lazy to try it out. I am glad I was right :slight_smile:

1 Like