解決方案是使用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)