fortran - 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
解决方案
推荐阅读
- c# - 使用列表的相同状态获取多个计数
- python - 熊猫 DataFrame 上的 NaN 替换引发 TypeError:未找到匹配的签名
- reactjs - React Hook setstate 运行两次
- rest - 为进程有问题但不是异常的请求更正 HTTP 状态代码
- hadoop - 为什么 Impala 扫描节点很慢(RowBatchQueueGetWaitTime)?
- arrays - Excel:视列而定,增加不同的列并将所有总和相乘
- javascript - 在渲染函数中使用参数反应事件处理程序:性能问题?
- spring - 无法在 Spring Data REST 请求中保存 OneToMany/ManyToOne 关系
- html - 如何在 HTML 代码中设置行奇数/偶数格式
- vim - vim -c 出现 %s 问题,出现“非法变量名”错误