Fortran READ into derived type not working with gfortran (Fortran runtime error: End of file)

189 Views Asked by At

I have a derived type in Fortran and I need to read values from a text file into that type. My problem is that my code is working fine with the Intel Fortran compiler as well as the NAG Fortran compiler, but GFortran exits with an error. The minimal working example is below.

Module my_mod
  Type T
    Real :: a
  End Type T

  Interface Read (Formatted)
    Module Procedure read_T
  End Interface
Contains
  Subroutine read_T(var, unit, iotype, v_list, iostat, iomsg)
    Class (T), Intent (Inout) :: var
    Integer, Intent (In) :: unit
    Character (*), Intent (In) :: iotype
    Integer, Intent (In) :: v_list(:)
    Integer, Intent (Out) :: iostat
    Character (*), Intent (Inout) :: iomsg

    Read (unit, *, iostat=iostat, iomsg=iomsg) var%a
  End Subroutine
End Module my_mod

Program main
  Use my_mod
  Implicit None
  Type(T) :: x
  Type(T) :: y

  Open(unit=20, file='data.txt', action='read')
  Read(20, *) x        ! Here GFortran fails because it somehow reaches EOF
  Write(*, *) x
  Read(20, *) y
  Write(*, *) y
End Program main

with the data.txt file:

1.0
2.0

With GFortran I get the error

At line 30 of file test.f90 (unit = 20, file = 'data.txt')
Fortran runtime error: End of file

Error termination. Backtrace:
#0  0x7f27a76fef2f in finalize_transfer
        at ../.././libgfortran/io/transfer.c:4175
#1  0x400aca in ???
#2  0x400b86 in ???
#3  0x7f27a69a8504 in ???
#4  0x4007e8 in ???
#5  0xffffffffffffffff in ???

One thing that works is to replace the Read statements as follows

Read(20, fmt='(DT)', advance='no') x        ! Works with all compilers :)
Write(*, *) x
Read(20, fmt='(DT)', advance='no') y        ! Works with all compilers :)
Write(*, *) y

So my question .. is it possible to get the same behaviour in GFortran without fmt='(DT)', advance='no'?

And who has the correct behavior? GFortran or Intel Fortran and NAG Fortran?

Thanks for any help,

Simon

0

There are 0 best solutions below