2

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.

Community
  • 1
  • 1
jiadong
  • 284
  • 5
  • 15

2 Answers2

3

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.

haraldkl
  • 3,753
  • 25
  • 44
1

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
Fortranner
  • 2,407
  • 2
  • 19
  • 25