You can't declare variables with unknown dimension "dimension(1:n)".
This program first reads the file to determine the number of rows:
program reading
implicit none
! variable declaration
real, dimension(:) ,allocatable :: x ,y
real :: dist
integer:: i, j ,n ,io
open(2, file = 'xyBVNData_R.txt', status = 'old', action = 'read')
n = 0
DO
READ(2,*,iostat=io)
IF (io/=0) EXIT
n = n + 1
END DO
print*, n
allocate( x(n) ,y(n) )
rewind(2)
DO i =1,n
READ(2,*) x(i),y(i)
END DO
dist=0.0
DO i=1,n
DO j=1,n
dist = dist + sqrt( (x(i)-x(j))**2 + (y(i)-y(j))**2 )
END DO
END DO
! writing and saving
DO i= 1, n
write(*,*) i, x(i)
END DO
end program reading
Another alternative (Fortran 2003), it reallocates the arrays x
and y
by adding the new element when a new line is read:
program reading
implicit none
! variable declaration
real, dimension(:) ,allocatable :: x ,y
real :: dist ,x_tmp ,y_tmp
integer:: i, j ,n ,io
open(2, file = 'xyBVNData_R.txt', status = 'old', action = 'read')
n = 0
allocate( x(0) ,y(0) )
DO
READ(2,*,iostat=io) x_tmp,y_tmp
x = [x,x_tmp]
y = [y,y_tmp]
IF (io/=0) EXIT
n = n + 1
END DO
print*, n
dist=0.0
DO i=1,n
DO j=1,n
dist = dist + sqrt( (x(i)-x(j))**2 + (y(i)-y(j))**2 )
END DO
END DO
! writing and saving
DO i= 1, n
write(*,*) i, x(i)
END DO
end program reading