module link_module implicit none type rlink real :: n type (rlink), pointer :: next=>null() end type rlink type ilink integer:: n type (ilink), pointer :: next=>null() end type ilink interface read_data module procedure read_real module procedure read_integer end interface contains subroutine read_real(x,n) implicit none type (rlink), pointer :: root, current integer :: i = 0 integer, intent(out):: n integer :: io_stat_number = 0 real, allocatable, dimension (:), intent(out) :: x character(len=40):: fname print *,'file containing real numbers?' read '(A)',fname open(unit=1,file=fname,status='old') allocate (root) ! read in 1st number from file read (unit=1,fmt=*,iostat=io_stat_number) root%n if( io_stat_number == 0)then ! not end of file i = i + 1 allocate (root%next) end if current => root ! read in remaining numbers (1 per line) do while (associated(current%next)) current => current%next read (unit=1,fmt=*,iostat=io_stat_number) current%n if (io_stat_number == 0) then i = i + 1 allocate (current%next) end if end do n = i allocate (x(1:n)) i = 1 current => root do while (associated(current%next)) x(i) = current%n i = i + 1 current => current%next end do close (unit=1) end subroutine read_real subroutine read_integer(x,n) implicit none type (ilink), pointer :: root, current integer :: i = 0 integer, intent(out):: n integer :: io_stat_number = 0 integer, allocatable, dimension (:), intent(out) :: x character(len=40):: fname print *,'file containing integer numbers?' read '(A)',fname open(unit=1,file=fname,status='old') allocate (root) ! read in 1st number from file read (unit=1,fmt=*,iostat=io_stat_number) root%n if( io_stat_number == 0)then ! not end of file i = i + 1 allocate (root%next) end if current => root ! read in remaining numbers (1 per line) do while (associated(current%next)) current => current%next read (unit=1,fmt=*,iostat=io_stat_number) current%n if (io_stat_number == 0) then i = i + 1 allocate (current%next) end if end do n = i allocate (x(1:n)) i = 1 current => root do while (associated(current%next)) x(i) = current%n i = i + 1 current => current%next end do close (unit=1) end subroutine read_integer end module link_module program ch2202c !ch2202a updated to fortran 95 use link_module implicit none real, allocatable, dimension(:):: rx integer, allocatable, dimension(:):: ix integer:: i,n call read_data(rx,n) print*,n,' real data values:' do i=1,n print*,rx(i) end do call read_data(ix,n) print *,n,' integer data values:' do i=1,n print*,ix(i) end do end program ch2202c