## Fortran - Output File names

Hi,

I looked around in some forums, but couldn't find a good answer to this problem: my programmes produce several output files. When the programm is finished, I add certain simulation parameters to the file name, so that I see later, to which simulation run this file belongs.
(for example, a file "result.dat" is renamed to "result_500.dat" because the 500 is a certain parameter in that programm).

Now i don't want to manually rename a lot of files, but instead I'm looking for a way to make Fortran incorporate certain variables in the file names.
I only know this way of declaration:
Code:
open(unit=1,status='unknown',file='result.dat')
Assuming I have a certain variable (e.g. an integer called "number")
Is it possible to assign a file name "result_*number*.dat", so that I only have to change the value of "number" and all files have that in their names?

 PhysOrg.com science news on PhysOrg.com >> New language discovery reveals linguistic insights>> US official: Solar plane to help ground energy use (Update)>> Four microphones, computer algorithm enough to produce 3-D model of simple, convex room
 Recognitions: Gold Member A possible solution , say you have 10 files with differnet names and you want to add the number 19 to all of them . Put the initial names(i.e., before adding 19) of these files in a character array defined as follow: CHARACTER(len=25),DIMENSION(10)::filenames also, define a character that contains the number(19) CHARACTER(len=2)::number number='19' !The quotqtion are important then to add the number to the names use the concatenation operator as follows: DO i=1,10 filename(i)=filaename(i) // number// '.dat' END DO now you have the names ready in the array, you can open ten files now with the statement: DO i=1,10 OPEN(UNIT=i+10,FILE=filename(i),STATUS='replace',ACTION='write') END DO Now you have ten files with the required names ready to write in them. May be this is not the best way, but I hope it works for you.
 Thanks for your answer! That's exactly what I'm looking for. I'm still having a little trouble though, because the numbers that should be in the filenames are integer values and I don't know how to convert them into characters. It's supposed to work with the simple write - command, but somehow I can't get it to work correctly :(

Recognitions:
Gold Member

## Fortran - Output File names

Hi angura,

You can use an internal file to do this. Say we have an integer i=567, and a character called number and we want to assign the value of i to the character then this piece of code will do the job:
..........
INTEGER::i
CHARACTER(len=10)::number
i=567
WRITE(number,100) i
100 FORMAT(I3)
........

A couple of comments, check that the length of the character is enough since this can create a runtime error. Also note that the write statement above creates a buffer file and do the conversion .
 Take care that the number has three digits when you use format I3. If you want numbers with a maximum of three digits, with leading zeroes for those below 100, you can use the Iw.m conversion, for example, Code: WRITE(number,100) i 100 FORMAT(I3.3) will produce 007 when i=7. This is important because you do not want a file name like: ACE 7.dat but rather ACE007.dat
 I also have the same problem. However, the answer doesn't work to me. I used gfortran to compile, then it said filename must be scalar. How can I fix this? or should I use another compiler?
 At which position does it complain, that filename should be scalar? Could you post a minimal example of your code?
 Recognitions: Science Advisor I typically write to a variable, using variable and strings, which I then open. Here is an example from a subroutine I have: Code: DO m=1,nFiles WRITE(fname,'(a,i4.4,a)')'grid',m,'.fast' OPEN(12,file=fname,form='formatted') ... ... END DO In this particular routine, I open this file which I already have, and modify it, writing out a formatted or post processed file. I concactanate the filenames easily as: Code:  oname = 'post.'//fname WRITE(6,*) 'OPENING: ',oname OPEN(13,file=oname,file='formatted') I 'used' to open several files and then write or open the file unit number as the do loop iterator, i.e. Code: DO i=1,10 OPEN(i,file='whatever') END DO But the flexibility of the way I'm doing it up there will save you a lot of hassle down the road. I recommend it.

 Similar discussions for: Fortran - Output File names Thread Forum Replies Programming & Comp Sci 2 Programming & Comp Sci 1 Programming & Comp Sci 1 Math & Science Software 17