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

Renaming a file by means of an integer tag

margmacd
Beginner
840 Views
Dear Sir/Madam

I would like to define a variable for naming a file within a write statement in such a way that this variable is updated each time I run a new loop of my program.

The new name would be of the form "filename"_irun, where only irun (the number of the current run) is being updated. As such, irun would be used as a tag to distinguish between different output files according to the run number. Can you please advise me how to do this with Compaq Visual Fortran. Since "filename" is a character variable and irun is an integer variable for a do loop of the form do irun =1 501
end do
and the write statement is contained within the above do loop the appropriate solution does not appear obvious to me.

Many thanks for your help.

Best wishes

Margaret MacDougall
0 Kudos
3 Replies
billaustralia
Beginner
840 Views
Use an internal write

character fname*7
do 10 i=1,9
write(fname,'(a,i1)')'prefix',i
10 continue
0 Kudos
margmacd
Beginner
840 Views
Dear Sir

Many thanks for your advice. I am sorry to say, however, that I am not clear how to implement the piece of code that you recommend. Suppose for, example, I wanted to write the word "Mathematics" to 9 files entitled fname1,...fname9. What should appear in my open statement and my write statement when I choose to write to the (9?) files which you appear to be defining. Presumably, I shall need a separate loop from the one which you suggest and my open and write statements will need to be inside this loop.

Also, what is the significance of the character fname*7 which you declare and where should I use it?

Best wishes

Margaret

0 Kudos
Jugoslav_Dujic
Valued Contributor II
840 Views

CHARACTER(20) sFileName

DO i=1,500
...some calculation
WRITE(sFileName, "(A, i3.3)") 'Mathematics', i
OPEN(11, FILE=sFileName)
WRITE(11) ...
CLOSE(11)
END DO
0 Kudos
Reply