【发布时间】:2014-06-12 08:27:48
【问题描述】:
我想从文件中获取数据,该文件的数据内容可以具有可变大小。但是,结构非常简单。 3 列和未定义的行数。我认为使用可分配的多维数组和显式 DO 循环可以解决我的问题。到目前为止,这是我的代码
program arraycall
implicit none
integer, dimension(:,:), allocatable :: array
integer :: max_rows, max_cols, row, col
allocate(array(row,col))
open(10, file='boundary.txt', access='sequential', status='old', FORM='FORMATTED')
DO row = 1, max_rows
DO col = 1, max_cols
READ (10,*) array (row, col)
END DO
END DO
print *, array (row,col)
deallocate(array)
end program arraycall
现在我面临的问题是我不知道应该如何定义这些 max_rows 和 max_cols,这与它的大小未知这一事实产生了共鸣。
示例文件可能看起来像
11 12 13
21 22 23
31 32 33
41 42 43
所以我想出了动态(动态)估计文件记录长度的方法。更新以供其他人参考
!---------------------------------------------------------------------
! Estimate the number of records in the inputfile
!---------------------------------------------------------------------
open(lin,file=inputfile,status='old',action='read',position='rewind')
loop1: do
read(lin,*,iostat=eastat) inputline
if (eastat < 0) then
write(*,*) trim(inputfile),": number of records = ", numvalues
exit loop1
else if (eastat > 0 ) then
stop "IO-Error!"
end if
numvalues=numvalues+1
end do loop1
!-----------------------------------------------------------------------
! Read the records from the inputfile
!-----------------------------------------------------------------------
rewind(lin)
allocate (lon(numvalues),lat(numvalues),value(numvalues))
do i=1,numvalues
read(lin,*) lon(i),lat(i),value(i)
end do
close(lin)
【问题讨论】:
标签: arrays fortran fortran90 dynamic-arrays