解决方案是使用Fortran 90/95/2003/2008,它具有解决问题所需的功能,而FORTRAN 77则没有。读取文件一次以确定数据项的数量。倒回文件。分配所需长度的数组。再次读取文件,读入数组。
使用的Fortran 2003/2008(未测试):
use iso_fortran_env
real :: xtmp, ytmp
real, dimension (:), allocatable :: x, y
integer :: i, n
integer :: Read_Code
open (unit=75, file=...)
n = 0
LengthLoop: do
read (75, *, iostat=Read_Code) xtmp, ytmp
if (Read_Code /= 0) then
if (Read_Code == iostat_end) then
exit LengthLoop
else
write (*, '(/ "read error: ", I0)') Read_Code
stop
end if
end if
n = n + 1
end do LengthLoop
allocate (x(n))
allocate (y(n))
rewind (75)
do i=1, n
read (75, *) x(i), y(i)
end do
close (75)