循环变量文件名 [英] Looping over variable file names
问题描述
我正在使用 Fortran 对拆分为多个文件的庞大数据集进行计算.文件的名称是:
I am using Fortran to do calculation on huge data set which was split into many files. The names of the files are:
maltoLyo12per-reimage-set1.traj
maltoLyo12per-reimage-set2.traj
maltoLyo12per-reimage-set3.traj
我编写的计算代码如下:
The code I wrote to do the calculation is as below:
fileLoop: do j = 31, 34
OPEN(unit=31,status='old',file=fileplace//'maltoLyo12per-reimage-set1.traj')
OPEN(unit=32,status='old',file=fileplace//'maltoLyo12per-reimage-set2.traj')
OPEN(unit=33,status='old',file=fileplace//'maltoLyo12per-reimage-set3.traj')
OPEN(unit=34,status='old',file=fileplace//'maltoLyo12per-reimage-set4.traj')
... operation....
close (j)
end do fileLoop
在运行期间,我希望代码一次打开每个文件并在完成计算后关闭它们.但是上面的代码会一次打开所有文件,计算完成后一个接一个地关闭.
During the run I want the code to open each file at a time and close them after finish calculation. But the above code will open all the files at once and close them one after one upon finish calculation.
所以我尝试修改如下代码:
So I tried to alter the code something like below:
fileLoop: do j = 31, 34
OPEN(unit=j,status='old',file=fileplace//'maltoLyo12per-reimage-set1.traj')
close (j)
end do fileLoop
但是在这里我遇到了文件名的问题.每次循环运行时,文件名都不会因为文件名中的短语set1"而改变.我希望文件名中的数字更改为 set1、set2、set3 等,随后文件单元编号为 31、32、33、34 等.
But here I am facing a problem with the file name. Each time the loop run, the file name doesn't change because of the phrase "set1" in the file name. I want the number in the file name to change like set1, set2, set3, etc., subsequently with file unit number 31,32,33,34, etc.
推荐答案
类似这样:(编辑为单元号 31 到 34,文件名 1 到 4.)
Something like this: (edited to have unit numbers 31 to 34, filenames 1 to 4.)
character (len=90) :: filename
fileLoop: do j = 31, 34
write (filename, '( "maltoLyo12per-reimage-set", I1, ".traj" )' ) j - 30
OPEN(unit=j,status='old',file=filename)
close (j)
end do fileLoop
这篇关于循环变量文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!