各位大牛,小弟期望能实现这样一个功能:
1. 在子程序里面读取一个文件(20+行,每行1个整数),并且把这20+个整数放入一个数组中,比如该数组名字为elem(:)。由于在读取之前不知道有几个元素,因此在子程序里面,定义elem(:)为动态数组。
2. 在子程序里面已经把elem做好,在主程序里面,如何调用该数组呢。
如果按照如下程序,则提示 forrtl: severe (174): SIGSEGV, segmentation fault occurred
错误的原因在于两次定义了allocatable 变量elem, 可是,如果不这么做,该怎么把子程序里面的elem(:)传回主程序呢?
[Fortran] 纯文本查看 复制代码 program main
integer, allocatable :: elem(:)
c
call assem(elem)
c
end
c--------------------------------------------------------------------------
c--------------------------------------------------------------------------
subroutine assem(elem)
implicit none
character(Len=100) tmp_str(1000)
integer,allocatable :: elem(:)
integer I,J
open(11,file='data.txt',status=old)
c--------------------------------------------------------------------------
c put the whole file into memory as character ...starts
c--------------------------------------------------------------------------
J = 0
do I = 1,4000
read(11,'(a)',end=101) tmp_str(I)
J = J +1
101 enddo
c
allocate(elem(J))
c
c--------------------------------------------------------------------------
c put the whole file into memory as character ...completed
c--------------------------------------------------------------------------
c
c--------------------------------------------------------------------------
c put the data into array elem(:) ...starts
c--------------------------------------------------------------------------
c do I = 1,J
read( tmp_str(I),*)elem(I)
enddo
c--------------------------------------------------------------------------
c put the data into array elem(:) ...completed
c--------------------------------------------------------------------------
end |