Fortran: opening variable file names

  • Fortran
  • Thread starter autobot.d
  • Start date
  • #1
68
0

Main Question or Discussion Point

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
 

Answers and Replies

  • #2
68
0
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.
 
  • #3
jtbell
Mentor
15,545
3,459
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 accomodate. 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"
 
  • #4
68
0
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
 
  • #5
68
0
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:
  • #6
rcgldr
Homework Helper
8,682
520
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:
  • #7
1,065
53
Code:
WRITE (filename, '(a,I3.3,a)') 'MatOut',N,'.dat'
OPEN (12, FILE=filename, FORM = 'FORMATTED')
 
  • #8
33,505
5,191
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..

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.
 
  • #9
1,065
53
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.
 

Related Threads on Fortran: opening variable file names

  • Last Post
Replies
7
Views
40K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
4
Views
2K
Replies
20
Views
32K
  • Last Post
Replies
4
Views
4K
Replies
1
Views
739
Replies
8
Views
13K
Replies
12
Views
13K
Top