Can Fortran Incorporate Variables in Output File Names?

In summary: Thanks for the feedback!In summary, this person is looking for a way to incorporate certain variables into the file names without having to rename them each time. They found a solution in which they use an internal file.
  • #1
angura
11
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
  • #2
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.
 
  • #3
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 :(
 
  • #4
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 .
 
  • #5
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
 
  • #6
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?
 
  • #7
At which position does it complain, that filename should be scalar?
Could you post a minimal example of your code?
 
  • #8
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.
 

1. What is an output file in Fortran?

An output file in Fortran is a file that contains data that has been generated or modified by a Fortran program. This data can be used for further analysis or transferred to other programs.

2. How do I specify an output file name in Fortran?

To specify an output file name in Fortran, you must include a file name in the OPEN statement of your program. This file name can be a literal string or a character variable.

3. Can I create multiple output files in a single Fortran program?

Yes, it is possible to create multiple output files in a single Fortran program. You can use different file names in the OPEN statement for each output file and write to them separately.

4. What happens if I try to open an existing output file in Fortran?

If you try to open an existing output file in Fortran, it will overwrite the existing file with the new data. To avoid this, you can use the STATUS specifier in the OPEN statement to specify whether the file should be opened as a new file or an existing file.

5. How can I check if an output file was successfully created in Fortran?

You can check if an output file was successfully created in Fortran by checking the IOSTAT variable after the OPEN statement. If the IOSTAT value is 0, the file was successfully opened and created. If not, you can check the error code to determine the cause of the error.

Similar threads

  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
8
Views
876
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
19
Views
5K
Back
Top