代码如下:
Module chainTableMod
implicit none
type :: real_value
real :: value
type( real_value ), pointer :: next
end type
type( real_value ), pointer :: head
type( real_value ), pointer :: tail
type( real_value ), pointer :: ptr
character(len=20) :: fname
integer :: istat, fileid
real :: temp
contains
subroutine chainTable
implicit none
write( *,'(1x,a)' ) 'please input the filename: '
read( *,'(a)' ) fname
open( newunit = fileid, file = trim(fname), iostat = istat )
fileopen: if ( istat == 0 ) then !.. open file successfully
input: do
read( fileid, *, iostat = istat ) temp
if ( istat /= 0 ) exit input
if ( .not. associated(head) ) then
allocate( head, stat = istat )
tail => head
tail%next => null()
tail%value = temp
else
allocate( tail%next, stat = istat )
tail => tail%next
nullify( tail%next )
tail%value = temp
end if
end do input
!.. write out the data
ptr => head
output: do
if ( .not. associated(ptr) ) exit
write( *,'(1x,f10.4)' ) ptr%value
ptr => ptr%next
end do output
else !.. open file failed
write( *,'(1x,a,g0)' ) 'file open failed--status = ', istat
end if fileopen
end subroutine chainTable
End module chainTableMod
Program main
use chainTableMod
implicit none
call chainTable
End program main
需要用到的文件名为:input.dat
文件input.dat数据如下:
1.1
2.34
0.0
4.6
7.2
5.6
8.2
1.9
4.2
3.3
4.1
执行代码,结果如下:
1.1
2.34
0.0
4.6
7.2
5.6
8.2
1.9
4.2
3.3
4.1