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

Simple Fortran Question: Incrementing a FILE name within a loop

Michael_Cupples
Principiante
3.119 Vistas
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 Solución
DavidWhite
Colaborador Valioso II
3.119 Vistas
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

Ver la solución en mensaje original publicado

3 Respuestas
DavidWhite
Colaborador Valioso II
3.120 Vistas
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
mecej4
Colaborador Distinguido III
3.119 Vistas
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]

Les_Neilson
Colaborador Valioso II
3.119 Vistas
Or just use the format I0 (that is zero)

Les
Responder