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

Simple Fortran Question: Incrementing a FILE name within a loop

Michael_Cupples
Beginner
2,964 Views
I am looping on IHIST in the open statement below. I want to change the file name 'test100.txt' on the during looping such that I get a set of files at the end such as test101.txt, test102.txt, ..., test1000.txt

Pointing me to a known solution would be much appreciated. Note, I am new to character manipulation in FORTRAN, so do not understand how to formulate this problem

OPEN (UNIT=100+IHIST, FILE='test100.txt', STATUS='UNKNOWN')

Mike
0 Kudos
1 Solution
DavidWhite
Valued Contributor II
2,964 Views
WRITE(filename,'(a,i4.4,a)') "TEST",i,".TXT"

will enable you to create filenames from TEST0000.TXT to TEST9999.TXT

I'm not sure if you can specify UNIT=100+IHIST - hopefully someone else will answer that for you.

David

View solution in original post

0 Kudos
3 Replies
DavidWhite
Valued Contributor II
2,965 Views
WRITE(filename,'(a,i4.4,a)') "TEST",i,".TXT"

will enable you to create filenames from TEST0000.TXT to TEST9999.TXT

I'm not sure if you can specify UNIT=100+IHIST - hopefully someone else will answer that for you.

David
0 Kudos
mecej4
Honored Contributor III
2,964 Views
For the syntax of the OPEN statement, see section 9.4 of the Standard. You can see there that the unit number is allowed to be specified as a scalar integer expression.

If you must have files named test100.txt,... rather than test0100.txt, ...:

[fortran]character*12 :: filename
integer :: iunit

if(iunit.LT. 1000)then
   write(filename,'("TEST",I3,".TXT")')iunit
   open(unit=iunit+100,file=filename,status='unknown')
   ...
   close(unit=iunit)
else
   write(filename,'("TEST",I4,".TXT")')iunit
   open(unit=iunit+100,file=filename,status='unknown')
   ...
   close(unit=iunit)
endif   [/fortran]

0 Kudos
Les_Neilson
Valued Contributor II
2,964 Views
Or just use the format I0 (that is zero)

Les
0 Kudos
Reply