0
votes

I am new to doing fortran programing. I would like to open multiple .txt files (CrossSec01 to CrossSec10) and read data an Allocatable arrays into a single file.

My code gives me "end of file error" in the "read". Can any novices out there help me, please?

Here's my code:

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
1
Are i,j, and k known/initialized at the time of the allocate statement? Are you reading in the correct number of elements from the file? I would start by adding stat/iostat to all I/O statements and to allocate to see whether each operations succeeds. - Alexander Vogt
Apparently you want the first dimension to be y, the second x and the third z, but you have i, j, k in the allocate statement. - M. S. B.
Thanks M.S.B the code worked. Cheers. - user3161781

1 Answers

0
votes
allocate (dat(i,j,k))

I think this is where the error occurs. You are allocating the array datwith dimensions i,j,k but i,j,k are not initialised yet. At this moment i,j and k have value as zero. This will make the array dat of zero dimensions and you can't write anything into it. I think you were trying to do allocate (dat(y,x,z)).