Fortran: opening variable file names

  • Context: Fortran 
  • Thread starter Thread starter autobot.d
  • Start date Start date
  • Tags Tags
    File Fortran Variable
Click For Summary

Discussion Overview

The discussion revolves around the challenges of dynamically generating file names in Fortran for saving 2-dimensional arrays during loop iterations. Participants explore various methods to concatenate strings and integers to form valid file names, addressing issues related to file naming conventions and output formatting.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks to create unique file names for each iteration of a loop, expressing difficulty in concatenating an integer to a character string.
  • Another participant suggests using an "internal write" technique to format the filename correctly, providing an example of how to structure the write statement.
  • A later reply indicates that while the first file name is generated correctly, subsequent names are not, leading to confusion over unexpected outputs.
  • Participants discuss the issue of files being named "fort.1" and "fort.2," with one suggesting that the problem lies in how the filename variable is referenced in the open statement.
  • There is mention of formatting issues when generating file names, particularly regarding padding numbers with zeros to maintain consistent file naming.
  • One participant emphasizes the importance of ensuring that the variable name is not enclosed in quotes when used in the open statement.

Areas of Agreement / Disagreement

There is no consensus on the best approach to resolve the file naming issues, as participants present differing viewpoints and solutions without a clear resolution to the problems raised.

Contextual Notes

Participants note potential limitations related to the formatting of file names, including the need for zero-padding and the distinction between string literals and variable names. There is also uncertainty about the directory where files are being created.

autobot.d
Messages
67
Reaction score
0
I am trying to write code so a 2-d array is written to the hard drive (which is later to be read). There are several do loop iterations and I would like the file name to change with every iteration so I get as many files as there are loop iterations. I get one 1-d array that is named A, no extension and that is the only one as well. The 1-d array is fine, I will just Shape to change it to a 2-d array, but the file names is getting me. Here is just a snippet of the code. If more is needed let me know. Thanks!



Code:
miscellaneous code here...
........
.......

integer :: n
CHARACTER :: filename,num

do i = 1,n

...

!Important part of code

filename = 'MatrixOut' //num//'.dat'  !num looks to be the problem, I tried putting an
                                                 !integer there that =i but that is an error



OPEN (UNIT = 8, FILE=filename, STATUS = 'REPLACE', IOSTAT = ierror)

do j=1,n
do k = 1,n

WRITE (8,"(1000(1x,f0.6))") (Matrix_Out(j,k))

end do 
end do
 
Technology news on Phys.org
I guess my real question is how to make a Character variable match what i, the index for the do loop, has for values then I can make the names of my files appropriately.

Thanks.
 
autobot.d said:
Code:
filename = 'MatrixOut' //num//'.dat'  !num looks to be the problem, I tried putting an
                                                 !integer there that =i but that is an error

You cannot concatenate an integer to a character string using the // operator, which can concatenate only strings. To construct the filename, use the "internal write" techinique, something like this:

Code:
write (filename, fmt='(a,i2,a)') 'MatrixOut', num, '.dat'

You may need to tweak the format, e.g. depending on how many digits you need to accommodate. I'm in a hurry at the moment so I can't test it myself right now.

For further information about this technique, try a Google search for "fortran internal write"
 
So using your advice the first file name is appropriately named MatrixOut1.dat
but the next matrix in the iteration is named 0u( , and u has two dots over it (german?)
and no file extension, and as far as I can tell right now both files have appropriate output in them.

Code:
do i=1,n  

...

do j=1,n
do k=1, n

WRITE (filename, fmt='(a,I2,a)') 'MatrixOut',i,'.dat'
OPEN (UNIT = 8, FILE=filename(i), STATUS = 'REPLACE', IOSTAT = ierror)
WRITE (8,"(1000(1x,f0.6))") (MatrixOut(j,k))
end do
end do
end do
 
So I have gotten to the point where my files are now named
fort.1
fort.2
and I have no idea why. Here are the nuts and bolts. I want them to be named for every N

Code:
Integer :: i, N
Real, Allocatable, Dimension(:,:) :: MatOut
Character(len=25) :: filename 

do i=1,2
N=100*(i*2)
...make matrix and so on...

WRITE (filename, '(a,I6,a)') 'MatOut',N,'.dat'
OPEN (12, FILE='filename', FORM = 'FORMATTED')

I am right on the cusp, this is the last thing I need to complete my program, so any help is appreciated. Thanks.
 
Last edited:
It's been a long time since I used Fortran, any chance this might work?
Code:
...
WRITE (filename, '(A6,I6,A4)') 'MatOut',N,'.dat'

gsal figured out what was wrong in the next post.
 
Last edited:
Code:
WRITE (filename, '(a,I3.3,a)') 'MatOut',N,'.dat'
OPEN (12, FILE=filename, FORM = 'FORMATTED')
 
autobot.d said:
So I have gotten to the point where my files are now named
fort.1
fort.2
and I have no idea why. Here are the nuts and bolts. I want them to be named for every N

Code:
Integer :: i, N
Real, Allocatable, Dimension(:,:) :: MatOut
Character(len=25) :: filename 

do i=1,2
N=100*(i*2)
...make matrix and so on...

WRITE (filename, '(a,I6,a)') 'MatOut',N,'.dat'
OPEN (12, FILE='filename', FORM = 'FORMATTED')

I am right on the cusp, this is the last thing I need to complete my program, so any help is appreciated. Thanks.
Your code above is not producing files named fort.1 and fort.2. It looks to me like you are confusing yourself. It's possible that the files are actually being created as you expect, but you're not looking for them in the directory where they live.

The write statement initializes a string variable (filename) to a string value of the form "MatOutDDD.dat", where the DDD part represents a three-digit number. When i = 1, N = 200, so the string would be "MatOut200.dat".

The open statement associates unit 12 with a file whose name is "filename". Note that the string literal "filename" and the variable filename have nothing to do with each other.

Edit: gsal beat me to the punch on the above.[/color].

When you compile your code, where do the compiler and linker put the executable? I would look in a directory named "bin". Your program might be creating files and putting them in the same directory as the executable.
 
Earlier, I did not have time, so I just posted the punchline.

What I thought was happening at the beginning is that

if 99 files are needed and the file name is composed as '(a,i2,a)', then the filename will have a blank space for when n <10

if 999 files are needed and the file name is composed as '(a,i3,a)', then the filename will have two blank spaces for when n <100

so, the trick for that is to pad with zeros on the left '(a,i3.3,a)'

Other than that, the files should be created in the same directory where the program is being run from...well, I am talking command line execution, here.

And yes, be careful about enclosing variable name filename in quotes..that's is not what you need.
 

Similar threads

  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
6K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 12 ·
Replies
12
Views
11K