Fortran: opening variable file names

In summary, Fortran is a high-level programming language commonly used for scientific and mathematical computing. It was first introduced in the 1950s and has since undergone several updates and revisions. To open a variable file name in Fortran, you can use the OPEN statement with the keyword "FILE". This will prompt the user to enter the file name during runtime, allowing for flexibility in file names. Fortran also allows for variable file names to be used for both reading and writing, with the desired access mode specified in the OPEN statement. However, entering an incorrect file name can result in an error and termination of the program, so it is important to handle errors and validate user input. There are also some limitations to using variable file names in
  • #1
autobot.d
68
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
  • #2
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
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"
 
  • #4
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
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
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
Code:
WRITE (filename, '(a,I3.3,a)') 'MatOut',N,'.dat'
OPEN (12, FILE=filename, FORM = 'FORMATTED')
 
  • #8
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..

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
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.
 

1. What is Fortran?

Fortran is a high-level programming language commonly used for scientific and mathematical computing. It was first introduced in the 1950s and has since undergone several updates and revisions.

2. How do I open a variable file name in Fortran?

To open a variable file name in Fortran, you can use the OPEN statement with the keyword "FILE". This will prompt the user to enter the file name during runtime, allowing for flexibility in file names.

3. Can I use a variable file name in Fortran for both reading and writing?

Yes, Fortran allows you to open a variable file name for both reading and writing. You can specify the desired access mode (READ or WRITE) in the OPEN statement.

4. What happens if the user enters an incorrect file name when using a variable file name in Fortran?

If the user enters an incorrect file name when using a variable file name in Fortran, an error will be thrown and the program will terminate. It is important to handle errors and validate user input to prevent unexpected behavior.

5. Are there any limitations to using variable file names in Fortran?

Yes, there are some limitations to using variable file names in Fortran. The maximum length of a file name may vary depending on the operating system and compiler. Additionally, some special characters may not be allowed in file names. It is important to consult the Fortran documentation for specific limitations.

Similar threads

  • Programming and Computer Science
Replies
1
Views
556
  • Programming and Computer Science
Replies
21
Views
298
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
4
Views
623
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
12
Views
7K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
4
Views
750
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top