how to use derived types with allocatable elements for I/O

From: Ted (tsariysk_at_craft-tech.com)
Date: 05/25/04


Date: 24 May 2004 16:05:23 -0700

Hi,
Is there a way to use user defined types with allocatable components
and write/read to a random access file? With portland group f90 if I
calculate the size of the structure myself (inquire fails because of
allocatable) I am able to write but read fails with:

PGFIO-F-/unformatted read/unit=9/error code returned by host stdio -
0.
 File name = test.da unformatted, direct access record = 1
 In source file da_alloc.f90, at line number 32

With intel compiler it fails at compile time:
 I/O for a derived type with pointer or hidden components is illegal

Is it a principal litmitation I'm trying to break? Is there a work
around?
Here is a baby code that ilustrate my problm (if allcoatable is
commented out the code works fine).

Thanks in advance for any help,
Ted

module slice
  type sl
     integer :: i4
     real*4 :: r4
     real*4,allocatable :: a(:)
     !real*4 :: a(3)
  end type sl
end module slice

program da_alloc
  use slice
  implicit none
  type(sl) :: s,s1
  integer :: lr8,s_reclen
  lr8=0

  call init_slice(s)
  
  !inquire(iolength=i_recl) s
  s_reclen=s_reclen+8
  print*, "s_reclen", s_reclen

 !write(*,'(a,i3,4f8.3)')'s ',s%i4,s%r4
  write(*,'(a,i3,4f8.3)')'s ',s%i4,s%r4,s%a(1:3)
  open(unit=9,file='test.da',form='unformatted',recl=s_reclen,access='direct')
  write(unit=9,rec=0) s
  write(unit=9,rec=1) s
  write(unit=9,rec=2) s
  close(9)

  open(unit=9,file='test.da',form='unformatted',recl=s_reclen,access='direct')
  read(unit=9,rec=0) s1
  close(9)
  !write(*,'(a,i3,4f8.3)')'s1 ',s%i4,s%r4
  write(*,'(a,i3,4f8.3)')'s1 ',s%i4,s%r4,s%a(1:3)
 
contains
  subroutine init_slice(s)
    implicit none
    integer :: li,lr4,lr8,la

    type (sl), intent(inout) :: s
    s%i4=1
    inquire(iolength=li) i4
    s%r4=4.
    inquire(iolength=lr4) r4
    
     allocate(s%a(1:3))
    inquire(iolength=lr8) r8
    s%a=2.
    s_reclen=li+lr4+3*lr8
    print*, 'li,lr4,la,s_reclen=',li,lr4,3*lr8,s_reclen

  end subroutine init_slice
end program da_alloc