Fortran Can Fortran Incorporate Variables in Output File Names?

AI Thread Summary
The discussion focuses on automating the renaming of output files in Fortran by incorporating simulation parameters into the filenames. A user seeks a method to dynamically rename files based on an integer variable, such as appending "_500" to "result.dat" to create "result_500.dat". A proposed solution involves using a character array to store the initial filenames and concatenating the integer parameter converted to a character string. The conversion can be achieved using an internal WRITE statement with a specified format to ensure proper character length and leading zeros if necessary. Some users encounter issues with the filename needing to be scalar, prompting further clarification on code structure and compiler compatibility. An example of a subroutine is shared, demonstrating effective filename concatenation and file handling within a loop, emphasizing the flexibility and efficiency of this approach for managing multiple files.
angura
Messages
8
Reaction score
0
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?

Thanks for your help.
 
Technology news on Phys.org
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 :(
 
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?
 
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 threads

Replies
25
Views
3K
Replies
3
Views
3K
Replies
6
Views
2K
Replies
5
Views
2K
Replies
2
Views
2K
Replies
2
Views
1K
Replies
19
Views
6K
Replies
2
Views
2K
Replies
11
Views
3K
Back
Top