这是一个简单的例子,基本上只是交换write
。read
请注意首先分配数组。
program test
implicit none
integer,parameter :: ium=1234
integer :: a=1, b=2, c=3, d=4, e=5
REAL*8,allocatable :: v(:), h(:), f(:)
integer,allocatable :: iv(:), jv(:)
integer :: i, stat
! Set values
allocate( v(a), h(b), f(b), iv(b+1), jv(a), stat=stat )
if (stat/=0) stop 'Cannot allocate memory!'
v=1.d0
h=2.d0
f=3.d0
iv = 4
jv = 5
write(*,*) v, h, f
! Write back
open(unit=ium, file='test',form='unformatted',status='replace',action='write')
write(ium) a, b, c, d, e
write(ium) (iv(i),i=1,b+1)
write(ium) (jv(i),i=1,a)
write(ium) (v(i),i=1,a)
write(ium) (f(i),i=1,b)
write(ium) (h(i),i=1,b)
close(ium)
! Scratch
deallocate( v, h, f, iv, jv )
!========================================
! Read in
open(unit=ium, file='test',form='unformatted',status='old',action='read')
read(ium) a, b, c, d, e
! Allocate arrays
allocate( v(a), h(b), f(b), iv(b+1), jv(a), stat=stat )
if (stat/=0) stop 'Cannot allocate memory!'
read(ium) (iv(i),i=1,b+1)
read(ium) (jv(i),i=1,a)
read(ium) (v(i),i=1,a)
read(ium) (f(i),i=1,b)
read(ium) (h(i),i=1,b)
close(ium)
write(*,*) v, h, f
deallocate( v, h, f, iv, jv )
end program
根据您的需要进行调整。