Fortran Fortran: opening variable file names

AI Thread Summary
The discussion focuses on generating unique file names for each iteration of a loop in Fortran when writing a 2-D array to the hard drive. The user initially struggles with concatenating an integer to a character string for the file name, leading to incorrect file naming. Suggestions include using an "internal write" technique to format the filename correctly, ensuring it incorporates the loop index properly. The user eventually encounters issues with files being named "fort.1" and "fort.2," which are attributed to confusion between the variable name and string literals. The final advice emphasizes checking the output directory and ensuring the filename variable is not enclosed in quotes.
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
Views
3K
Replies
12
Views
3K
Replies
4
Views
2K
Replies
5
Views
5K
Replies
20
Views
3K
Replies
1
Views
3K
Replies
12
Views
10K
Back
Top