Hi All,
I'm trying to save a write a derived type that contains a pointer to an unformatted file for later reading. Below is a test version. The derived type sol in module dType is separate because I'm patching together various Fortran codes (F95 and F77, etc). The current version of the code "saves" sol1, but it seems to only save the pointer references, not their values, so when sol1 is deallocated, sol2 also becomes unreferenced. I'm sure my hack on the UDTIO is not correct.
Any help would be appreciated (I'm still trying to figure out UDTIO). Thanks
-joe
module dType implicit none type, public :: sol integer:: n real(8), pointer:: vec(:) end type sol end module dType module dType_io use dType implicit none contains subroutine write_sol(dtv, unit, iostat, iomsg) class(sol), intent(in) :: dtv integer, intent(in) :: unit integer, intent(out) :: iostat character(*), intent(inout) :: iomsg write(unit, iostat=iostat, iomsg=iomsg) dtv%n write(unit, iostat=iostat, iomsg=iomsg) size(dtv%vec) write(unit, iostat=iostat, iomsg=iomsg) dtv%vec end subroutine write_sol subroutine read_sol(dtv, unit, iostat, iomsg) class(sol), intent(inout) :: dtv integer, intent(in) :: unit integer, intent(out) :: iostat character(*), intent(inout) :: iomsg integer allocSize ! read is sol type, but allocate pointer space as needed. read(unit, iostat=iostat, iomsg=iomsg) dtv%n read(unit, iostat=iostat, iomsg=iomsg) allocSize allocate( dtv%vec(allocSize) ) read(unit, iostat=iostat, iomsg=iomsg) dtv%vec end subroutine read_sol end module dType_io program directAccess use dType implicit none integer, parameter :: dp = kind(0d0) type(sol):: sol1, sol2 integer solUnit, n, sze integer i sol1%n = 10 allocate( sol1%vec(sol1%n) ) sol1%vec = [ (real(i),i=1,sol1%n) ] open(newunit=solUnit, file='test.sol', form='unformatted', status='replace', action='write') write(solUnit) sol1 close(solUnit) open(newunit=solUnit, file='test.sol', form='unformatted', status='old', action='read') read(solUnit) sol2 deallocate( sol1%vec ); nullify(sol1%vec) ! this will cause sol2 to lose reference close(solUnit) stop end program directAccess