循环使用变量文件名

循环使用变量文件名

本文介绍了循环使用变量文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Fortran来对大量数据集进行计算,这些数据集被分成许多文件。这些文件的名称是:

  maltoLyo12per-reimage-set1.traj 
maltoLyo12per-reimage-set2.traj
maltoLyo12per-reimage-set3.traj

我写的计算代码如下:

  fileLoop:do j = 31,34 

OPEN(unit = 31,status ='旧的',file = fileplace //'maltoLyo12per-reimage-set2.traj')
OPEN(unit = 32,status ='old',file = fileplace //'maltoLyo12per-reimage-set2.traj')
OPEN(单元= 34,status ='old',file = fileplace /文件位置/'old',file = fileplace //'maltoLyo12per-reimage-set3.traj' (')

...操作....

在运行期间,我希望代码一次打开每个文件并在完成计算后关闭它们。但是,上面的代码会一次打开所有文件,并在计算完毕后一次性关闭它们。



所以我试着修改下面的代码:

  fileLoop:do j = 31,34 

OPEN(unit = j,status ='old',file = fileplace //'maltoLyo12per-reimage-set1.traj')

close(j)
end fileLoop

但是在这里我遇到了文件名的问题。每次循环运行时,文件名都不会因文件名中的短语set1而改变。我希望文件名中的数字像set1,set2,set3等那样改变,随后使用文件单元号31,32,33,34等。

解决方案

类似这样的:(编辑为单元号31至34,文件名1至4)。 c $ c> character(len = 90):: filename

fileLoop:do j = 31,34

write(filename,'(maltoLyo12per-reimage-set ,I1,.traj)')j - 30
OPEN(单位= j,status ='old',file =文件名)

关闭(j)
结束做fileLoop


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

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.

解决方案

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

这篇关于循环使用变量文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 00:33