Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran: opening variable file names

  1. Sep 27, 2012 #1
    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 (Text):

    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

  2. jcsd
  3. Sep 27, 2012 #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.

  4. Sep 27, 2012 #3


    User Avatar

    Staff: Mentor

    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 (Text):

    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"
  5. Sep 27, 2012 #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 (Text):

    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

  6. Sep 27, 2012 #5
    So I have gotten to the point where my files are now named
    and I have no idea why. Here are the nuts and bolts. I want them to be named for every N

    Code (Text):

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

    do i=1,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: Sep 27, 2012
  7. Sep 27, 2012 #6


    User Avatar
    Homework Helper

    It's been a long time since I used Fortran, any chance this might work?
    Code (Text):

    WRITE (filename, '(A6,I6,A4)') 'MatOut',N,'.dat'
    gsal figured out what was wrong in the next post.
    Last edited: Sep 27, 2012
  8. Sep 27, 2012 #7
    Code (Text):

    WRITE (filename, '(a,I3.3,a)') 'MatOut',N,'.dat'
    OPEN (12, FILE=filename, FORM = 'FORMATTED')
  9. Sep 27, 2012 #8


    Staff: Mentor

    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.
  10. Sep 28, 2012 #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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Fortran: opening variable file names