首页 > 解决方案 > Fortran 中的多维数组

问题描述

我编写了一个读取多个文件的 Fortran 代码。每个文件有 3 列和 1448 行。我想逐行读取文件,并将 column3[i] 的值保存到文件中。在 Python 中,我会一个一个地读取文件,获取 column3 中的第 i 个值并附加到已初始化的文件中。我不确定我会如何在 Fortran 中做到这一点。

module variables
type string
character(len=99), allocatable :: str 
end type string
end module variables

program readfiles
use variables
implicit none

real :: coorda, coordb, coordc
integer :: i, j, N, zeta, lam, k, rows
Logical, Save :: first_time = .True.
CHARACTER(len=100) :: FN 
type(string) :: array(3)
N=3 !--arbitrary number of files 
lam=80
array(1)%str = '2e8'
array(2)%str = '2e9'
array(3)%str = '3e9'

if(first_time) then
open (unit=98, file='output.txt')
DO I=1,N
lam = lam + 60
zeta=20
do j=1,N
    zeta = zeta + 20
    do k=1,N
                    WRITE(FN,10)lam, zeta, (array(k)%str)!,k=1,N)

                    OPEN(99,FILE=FN, action='read', status='old', position='rewind')!open the file to read
                   do rows=1, 1448
                   read (99,*) coorda, coordb, coordc
                   write (98, '(f13.10)') coordc     !this gives the formatting. 10 digits after decimal??
                   end do

                    CLOSE(99)   !this ensures it closes d file it is reading from so a new one can b opened for reading          

    enddo
enddo
END DO
close(98)
10 FORMAT('4e3_2048_',(I3.0),'_',(I2.2),'_',(A3),'.ksz_cl.txt') !length of this is decided by FN
endif

end program readfiles 

标签: fortrangfortran

解决方案


推荐阅读