How to name output files, making it related to a variable (in fortran)? [duplicate]

心不动则不痛 提交于 2019-12-06 15:25:07

So, here is a suggestion:

integer :: m
integer :: fu
character(len=10) :: file_id
character(len=50) :: file_name

! Write the integer into a string:
write(file_id, '(i0)') m

! Construct the filename:
file_name = 'file' // trim(adjustl(file_id)) // '.dat'

! Open the file with this name
open(file = trim(file_name), unit = fu)

Note, that you could also obtain leading zeroes with the iX.Y format string.

Googling "internal write fortran" shows how to create strings that embed the integer variable, which is demonstrated in the following program, which creates the string "file1.dat".

program internal_write
character (len=10) :: file_name
write (file_name,"('file',i0,'.dat')") 1
print*,"file name is ",trim(file_name)
end program internal_write
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!