在一维数组中一个接一个地读取文本文件列

问题描述 投票:0回答:0

我尝试使用 Fortran 阅读以下文本文件。文本文件有 24 行和 15 列。我想将所有数据放在一个单列数组 z 中。我想阅读第一栏,然后是第二栏,然后是第三栏,直到最后。 这是我的数据:

1   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0.16    0.16    0.17    0.18    0.19    0.19    0.19    0.19    0.18    0.18    0.18    0.18
0   0   0   0.31    0.33    0.35    0.37    0.37    0.39    0.39    0.37    0.37    0.37    0.37    0.37
0   0   0   0.47    0.49    0.52    0.55    0.56    0.58    0.58    0.56    0.55    0.55    0.55    0.55
0   0   0   0.48    0.51    0.55    0.59    0.6 0.61    0.61    0.59    0.59    0.59    0.59    0.59
0   0   0   0.48    0.53    0.57    0.62    0.63    0.64    0.63    0.62    0.62    0.62    0.62    0.62
10  34  0   0.49    0.55    0.6 0.66    0.68    0.69    0.68    0.67    0.67    0.67    0.67    0.67
0   0   0   0.49    0.57    0.63    0.7 0.72    0.73    0.73    0.72    0.72    0.72    0.72    0.72
0   0   0   0.49    0.59    0.65    0.75    0.77    0.78    0.77    0.77    0.77    0.77    0.77    0.77
0   0   0   0.49    0.61    0.68    0.79    0.81    0.83    0.82    0.81    0.82    0.82    0.82    0.82
0   0   0   0.49    0.63    0.7 0.83    0.86    0.87    0.87    0.86    0.87    0.87    0.87    0.87
0   0   0   0.49    0.65    0.73    0.87    0.9 0.92    0.92    0.91    0.92    0.92    0.92    0.92
0   0   0   0.49    0.67    0.76    0.91    0.95    0.97    0.96    0.95    0.97    0.97    0.97    0.97
0   0   0   0.49    0.69    0.78    0.95    0.99    1.01    1.01    1   1.02    1.02    1.02    1.02
0   0   0   0.49    0.71    0.81    0.99    1.04    1.06    1.06    1.05    1.07    1.07    1.07    1.07
0   0   0   0.49    0.73    0.83    1.04    1.08    1.11    1.1 1.09    1.11    1.11    1.11    1.11
0   0   0   0.49    0.75    0.86    1.08    1.13    1.15    1.15    1.14    1.16    1.16    1.16    1.16
0   0   0   0.49    0.77    0.88    1.12    1.18    1.2 1.2 1.19    1.21    1.21    1.21    1.21
0   0   0   0.49    0.77    0.88    1.12    1.18    1.2 1.2 1.19    1.21    1.21    1.21    1.21
0   0   0   0.49    0.77    0.88    1.12    1.18    1.2 1.2 1.19    1.21    1.21    1.21    1.21
24  48  72  0.49    0.77    0.88    1.12    1.18    1.2 1.2 1.19    1.21    1.21    1.21    1.21

这是我试过的代码:

program readdata
  use, intrinsic :: iso_fortran_env
  implicit none

  integer :: i, j, k
  real :: z(360)
  integer :: iostat
  character(len=255) :: filename

  ! Set the text file name
  filename = 'D:/Shuvashish/Oyster Shaye/growth_z_for_sal_tem.txt'

  ! Open the text file
  open(15, file=filename, status='old',form='formatted')

  ! Read the data from the text file into the z array
  k = 1
  do j = 1, 15   ! j is changing
    do i = 1, 24 ! when j=2 and k=25, iostat becomes -1
      read(15,*,iostat=iostat) z(k)
      if (iostat < 0) exit ! exit loop if iostat is negative
      k = k + 1
    end do

  end do

  ! Close the text file
  close(15)

  ! Print the values of z
  do k = 1, 360
    print*, z(k)
  end do

end program readdata

每当循环完成读取第一列的最后一个元素时,

iostat
变为 -1,之后它不会读取其他列。我是 Fortran 的绝对初学者。请让我知道如何使代码工作。谢谢。

file-io fortran text-files
© www.soinside.com 2019 - 2024. All rights reserved.