[J3] Interaction between user-defined input and list-directed input

Thomas König tk at tkoenig.net
Sat Apr 30 07:21:34 UTC 2022

Hi Malcolm,

 > This is quite tricky.

Indeed, thanks for your explanation.  I find it convincing (but I do not
think that the standard makes it clear).

The second example

module x
    implicit none
    type, public:: foo
       character(len=2) :: c
    end type foo
    interface read(formatted)
       module procedure read_formatted
    end interface read(formatted)
    subroutine read_formatted (dtv, unit, iotype, vlist, iostat, iomsg)
      class (foo), intent(inout) :: dtv
      integer, intent(in) :: unit
      character (len=*), intent(in) :: iotype
      integer, intent(in) :: vlist(:)
      integer, intent(out) :: iostat
      character (len=*), intent(inout) :: iomsg
      read (unit,'(A)',iostat=iostat,iomsg=iomsg) dtv%c
    end subroutine read_formatted
end module x

program main
    use x
    implicit none
    type(foo) :: a, b
    open (10,file="testfile.dat",status="replace")
    write (10,'(A)') '','aa bb'
    rewind (10)
    read (10,*) a%c, b%c
    write (*,'(10(A))') "Component read   : a = '",a,"' , b = '", b, "'"
    rewind (10)
    a%c = "x"
    b%c = "y"
    read (10,*) a, b
    write (*,'(10(A))') "User-defined read: a = '",a,"' , b = '", b, "'"
end program main

results in

Component read   : a = 'aa' , b = 'bb'
User-defined read: a = 'aa' , b = 'bb'


Component read   : a = 'aa' , b = 'bb'
User-defined read: a = '  ' , b = '  '

depending on the compiler.  The question then is the treatment
of the empty line at the beginning.

My guess, based on the standard's statement that end of record are to be
treated as blanks in list-directed I/O, is that the second variant is a
compiler bug, but I am not sure.  Or is this left to the implementation?

Best regards


More information about the J3 mailing list