Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

fortran90 - Read a file with an unknown number rows in Fortran

Heroes the new code I used. I have tried this, and it works if I have n declared first, which is not what I want. I need to know the total number of rows (n) and use that number afterward in my simulation. However, in variable declaration I need to have the diminution of my xy(n) before reading the data and if I do that, the code does not run.

the data file is two column of randomly simulated normal data

lets say something like this

1  3
2  4
3   6
4  8
5   9
6  8
7   1
8  9
99  88

I tried the following code to determine n but it did not work!

     program reading

implicit none
integer,allocatable :: a(:,:)
  integer :: pair(2)
  integer :: unit, n, io,k
!!!variable declaration 

 real, dimension(1:n)::x,y
 integer:: T, I1, I2, I3, i, j, dist
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
CLOSE (2)
  print*, n



 DO i=1,n!!!
  DO j=1,n!!
 dist=0.0
 DIST = dist+(sqrt((x(i)-x(j))**2 + (y(i)-y(j))**2))

 ENDDO
     ENDDO

 !!! writing and saving 
 Do i= 1, n 
  write(*,*) i, x(i)
 ENDDO
end program reading
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...