在Fortran中读取行号未知的文件 [英] Read a file with an unknown number rows in Fortran

查看:261
本文介绍了在Fortran中读取行号未知的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

英雄我使用的新代码.我已经尝试过了,如果我先声明了n,那么它就可以了,这不是我想要的.我需要知道总行数 (n),然后在模拟中使用该数字.但是,在变量声明中,我需要在读取数据之前缩小xy(n),如果这样做,代码将无法运行.

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

让我们这样说

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

我尝试了以下代码来确定n,但是它不起作用!

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

推荐答案

您不能声明维数为"dimension(1:n)"的变量.

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

另一种替代方法(Fortran 2003),它在读取新行时通过添加新元素来重新分配数组xy:

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

这篇关于在Fortran中读取行号未知的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆