打开并读取多个.txt文件

问题描述 投票:1回答:1

我不熟悉fortran编程。我想打开多个.txt文件(从CrossSec10到CrossSec10),并将可分配数组的数据读取到单个文件中。

我的代码在“读取”中给了我“文件结束错误”。请问有什么新手可以帮助我吗?

这是我的代码:

implicit none
 real(8), allocatable :: dat(:,:,:)    ! a 3D array, no defined size yet
 integer :: i,j,k,m,n                     
 integer :: x,y,z   

 open(unit=123,file="crossSec01.txt") ! opens 1st file reads & closes file
  !read (100,20) ((dat(i,j,k)
  read(123,*) y,x
  z=30
  close(123)

  allocate (dat(i,j,k))


  do k=1,z
  write(str,'("crossSec",i2.2,".txt")')k
  open(unit=345,file=str,status="old")
  read(345,*) 
  read(345,*)
  read(345,*)

  do i = 1,y
  read(345,*)(dat(i,j,k),j=1,x) ! This is where I get my fortran runtime error "end of file" error.

  end do
    close(345)
  end do
fortran fortran90
1个回答
0
投票
allocate (dat(i,j,k))

我认为这是发生错误的地方。您正在分配尺寸为dat的数组i,j,k,但i,j,k尚未初始化。此时,i,j和k的值为零。这将使数组dat的尺寸为零,并且您无法在其中写入任何内容。我认为您正在尝试执行allocate (dat(y,x,z))

© www.soinside.com 2019 - 2024. All rights reserved.