调用子程序为读取数据为动态数组赋值报错
我在调用子程序读入数据赋值到动态数组中时,一赋值就报 访问冲突,一直搞不懂问题的原因是什么,求助program main
implicit none
character,allocatable:: A(:,:)
integer:: m,n,nx,ny
character(len=200):: filename
integer:: i,j,iunit,mbc
filename="d:\2.txt"
nx=2
ny=2
allocate(A(nx,ny))
call read_txt(A,1,filename,m,n,nx,ny)
print*,A
end program
subroutineread_txt(input,iunit,filename,m,n,nx,ny)
implicit none
!函数内部变量声明
integer:: iunit,m,n
integer:: nx,ny
character(len=200):: filename
character(len=200):: input(nx,ny)
!局部变量
integer:: i,j
character(len=200):: line
character,allocatable:: temp(:)
open(unit=iunit,file=filename)
read(iunit,"(a)")line
read(line,*)m,n
allocate(temp(n))
do i=1,m
read(iunit,"(a)")line
read(line,*)(temp(j),j=1,n)
do j=1,n
!print*,temp(j)
input(i,j)=temp(j)
enddo
enddo
close(iunit)
end subroutine
实参的字符串长度是1
character,allocatable:: A(:,:)
虚参的字符串长度是200
character(len=200):: input(nx,ny)
自然就错了。
另外,你为什么先读到 temp 里?不能直接读到 input 里吗?
subroutineread_txt(input,iunit,filename,m,n,nx,ny)
implicit none
!函数内部变量声明
integer:: iunit,m,n
integer:: nx,ny,i
character(len=200):: filename
character:: input(nx,ny)
!局部变量
open(unit=iunit,file=filename)
read(iunit,*) m,n
do i = 1 , m
read(iunit,*) input(i,:n)
end do
end subroutine 胡文刚 发表于 2019-5-29 15:20
实参的字符串长度是1
character,allocatable:: A(:,:)
虚参的字符串长度是200
啊啊,就是这样,万分感谢,我自己马大哈了,没注意到。之前为了排除问题就先读入了temp。再次感谢!!
页:
[1]