I am trying to read an ASCII file and am getting errors when compiling such as:
Error: Syntax error in READ statement at (1)
And
Error: Allocatable array 'pos' at (1) must have a deferred shape or assumed rank
My code:
subroutine read_file(pos,mass,rho,vel,n)
integer :: i, n
real, allocatable, intent(out) :: pos(3,n), mass(n), rho(n), vel(3,n)
open(unit=11,file="star.ascii",status="old",action="read")
n = 0
do
read(unit=11,*)
n = n+1
enddo
allocate(pos(3,n), mass(n), rho(n), vel(3,n))
do i = 1,n
read(unit=11,*) pos(:,i), mass(i), rho(i), vel(:,i)
enddo
close(unit=11)
end subroutine read_file
The first 8 columns in my ascii file are the x, y, z components of position, mass, density, and the x, y, z components of velocity which I am reading into arrays, with (1,n), (2,n), (3,n) being the x, y, and z components correspondingly and n is supposed to be the number of particles.
What am I doing wrong and how can I make this code compile?
Update: first error solved, but still getting the same syntax error with the READ statement.
subroutine read_file(pos,mass,rho,vel,n)
integer :: i, n, ios
real, allocatable, intent(out) :: pos(:,:),mass(:),rho(:),vel(:,:)
open(unit=11,file="star.ascii",status="old",action="read")
n = 0
do
read(unit=11,*,iostat=ios) pos,mass,rho,vel
if (ios /= 0) exit
n = n+1
enddo
allocate(pos(3,n), mass(n), rho(n), vel(3,n))
rewind(11)
do i = 1,n
read(unit=11,*)pos(:,i),mass(i),rho(i),vel(:,i)
enddo
close(unit=11)
end subroutine read_file