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

Malcolm Cohen malcolm at nag-j.co.jp
Mon May 2 09:43:11 UTC 2022


Hi Thomas,

> 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?

It is arguable that the first variant is a compiler bug, as the list-directed READ has no data items before the defined i/o item, so it should leave positioning up to the procedure.

It's also arguable that the compiler should position to the first non-blank, to satisfy the rules about skipping leading blanks. I don't agree - I think the standard says the file position is unchanged on invocation of the subroutine.

There is a third variant, where the program aborts saying that the defined i/o procedure returned EOR during read. I don't think it should, as list-directed *should* treat an EOR return the same as "we got a blank", but the standard does not establish that clearly. So that too is arguable.

Anyway, the "eor are treated as blanks in list-directed" statement only applies to i/o performed by the parent i/o statement, not the child i/o statement (unless the child also does list-directed).

For list-directed defined i/o of a derived type, there is no point in duplicating what ordinary list-directed i/o does. IMO it should be doing something different, e.g. for this type it should establish that the i/o form is e.g. T#xx# (T being the name of the type, # to make it obvious that "normal" i/o is not going on here). Then the input read can just skip blanks (and EOR) until it finds the EOF (return EOF) or nonblank; if it's not "T", it should make up its own error return (good luck finding an iostat code that's not in use!), and if it is well then it can read the thing. It is possible (if perhaps tedious) to write usable defined-i/o procedures with that kind of design.

Cheers,
-- 
..............Malcolm Cohen, NAG Oxford/Tokyo.

-----Original Message-----
From: J3 <j3-bounces at mailman.j3-fortran.org> On Behalf Of Thomas Konig via J3
Sent: Saturday, April 30, 2022 4:22 PM
To: General J3 interest list <j3 at mailman.j3-fortran.org>
Cc: Thomas König <tk at tkoenig.net>; Malcolm Cohen <malcolm at nag-j.co.jp>
Subject: Re: [J3] Interaction between user-defined input and list-directed input

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)
contains
    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'

or

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

     Thomas





More information about the J3 mailing list