Creating file name strings

Creating file name strings

Hi All,

I'm trying to create numerous files in a directory with a do loop using the command open. I don't exactly know how I'm supposed to take care of the names of the files. Since I can only type a single string in the file="..." section of the open command

Any help would be highly appreciated

Regards

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
Repeat Offender's picture
program P
   implicit none
   integer n
   character(20) filename
   do n = 1, 10
      write(filename,'(a,i0,a)') 'file',n,'.txt'
      open(10,file=filename,status='replace')
      write(10,*) 'This is file number', filename
      close(10)
   end do
end program P

Output:

type file*.txt

file1.txt


 This is file numberfile1.txt

file10.txt


 This is file numberfile10.txt

file2.txt


 This is file numberfile2.txt

file3.txt


 This is file numberfile3.txt

file4.txt


 This is file numberfile4.txt

file5.txt


 This is file numberfile5.txt

file6.txt


 This is file numberfile6.txt

file7.txt


 This is file numberfile7.txt

file8.txt


 This is file numberfile8.txt

file9.txt


 This is file numberfile9.txt

 

Although it may LOOK like you can only type a single string, if you read the documentation carefully you can use a single string variable as well. Then, you have all the power of Fortran available to construct the needed characters in the string variable. The response above illustrates a specific example of this technique. You can also do some of the string construction "in place,", e.g. FILE = 'str1' // 'str2' // '.ext'.

Login to leave a comment.