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
  • 318
  • 5
  • 16
  • Write the integer into an array and adjustl that string, the concatanate it with the pre- and post-fix? – haraldkl Mar 27 '14 at 17:27
  • @haraldkl I do not need to write the integer into arrays ,but just want to name the output files, making these files include specific value of m, therefore they are distinguishable. – jiadong Mar 27 '14 at 17:45
  • You should show us some code, what you have tried so far. – Bálint Aradi Mar 27 '14 at 19:16
  • Sorry, didn't want to say array but character string... – haraldkl Mar 27 '14 at 21:21
  • 3
    See http://stackoverflow.com/questions/1262695/converting-integers-to-strings-in-fortran and http://stackoverflow.com/questions/16291270/looping-over-variable-file-names – M. S. B. Mar 27 '14 at 21:42

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,809
  • 26
  • 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,525
  • 2
  • 22
  • 25