Eg. The integer variable (m) will take the following values one by one:
1, 2, 3, ....
For each value of m, an array
p(i) (i=1,2,..., 1000)
is obtained and written in a output file with
open()
and write()
Could you tell me how to name these output files as
file1.dat, file2.dat, file3.dat, …
Thanks.
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
来源:https://stackoverflow.com/questions/22694626/how-to-name-output-files-making-it-related-to-a-variable-in-fortran