Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Creating file name strings

Mohammadjafar_S_
Beginner
3,181 Views

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

0 Kudos
2 Replies
JVanB
Valued Contributor II
3,181 Views
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

 

0 Kudos
dboggs
New Contributor I
3,181 Views

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'.

0 Kudos
Reply