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

Fortran - Output File names

  1. Jan 30, 2009 #1

    I looked around in some forums, but couldn't find a good answer to this problem: my programmes produce several output files. When the programm is finished, I add certain simulation parameters to the file name, so that I see later, to which simulation run this file belongs.
    (for example, a file "result.dat" is renamed to "result_500.dat" because the 500 is a certain parameter in that programm).

    Now i don't want to manually rename a lot of files, but instead I'm looking for a way to make Fortran incorporate certain variables in the file names.
    I only know this way of declaration:
    Code (Text):
    Assuming I have a certain variable (e.g. an integer called "number")
    Is it possible to assign a file name "result_*number*.dat", so that I only have to change the value of "number" and all files have that in their names?

    Thanks for your help.
  2. jcsd
  3. Jan 30, 2009 #2
    A possible solution , say you have 10 files with differnet names and you want to add the number 19 to all of them . Put the initial names(i.e., before adding 19) of these files in a character array defined as follow:

    also, define a character that contains the number(19)
    number='19' !The quotqtion are important

    then to add the number to the names use the concatenation operator as follows:
    DO i=1,10
    filename(i)=filaename(i) // number// '.dat'
    END DO

    now you have the names ready in the array, you can open ten files now with the statement:
    DO i=1,10
    END DO

    Now you have ten files with the required names ready to write in them.

    May be this is not the best way, but I hope it works for you.
  4. Jan 31, 2009 #3
    Thanks for your answer! That's exactly what I'm looking for.
    I'm still having a little trouble though, because the numbers that should be in the filenames are integer values and I don't know how to convert them into characters.
    It's supposed to work with the simple write - command, but somehow I can't get it to work correctly :(
  5. Jan 31, 2009 #4
    Hi angura,

    You can use an internal file to do this. Say we have an integer i=567, and a character called number and we want to assign the value of i to the character then this piece of code will do the job:
    WRITE(number,100) i
    100 FORMAT(I3)

    A couple of comments, check that the length of the character is enough since this can create a runtime error. Also note that the write statement above creates a buffer file and do the conversion .
  6. Jan 31, 2009 #5
    Take care that the number has three digits when you use format I3.
    If you want numbers with a maximum of three digits, with leading zeroes for those below 100, you can use the Iw.m conversion, for example,
    Code (Text):
    WRITE(number,100) i
    100 FORMAT(I3.3)
    will produce 007 when i=7.
    This is important because you do not want a file name like:
    ACE 7.dat
    but rather
  7. Jan 25, 2010 #6
    I also have the same problem.

    However, the answer doesn't work to me.
    I used gfortran to compile, then it said filename must be scalar.
    How can I fix this? or should I use another compiler?
  8. Jan 27, 2010 #7
    At which position does it complain, that filename should be scalar?
    Could you post a minimal example of your code?
  9. Jan 27, 2010 #8


    User Avatar
    Science Advisor

    I typically write to a variable, using variable and strings, which I then open. Here is an example from a subroutine I have:
    Code (Text):

    DO m=1,nFiles

    END DO
    In this particular routine, I open this file which I already have, and modify it, writing out a formatted or post processed file. I concactanate the filenames easily as:
    Code (Text):

      oname = 'post.'//fname
      WRITE(6,*) 'OPENING:  ',oname
    I 'used' to open several files and then write or open the file unit number as the do loop iterator, i.e.
    Code (Text):

    DO i=1,10
    END DO
    But the flexibility of the way I'm doing it up there will save you a lot of hassle down the road. I recommend it.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Fortran - Output File names
  1. Read file in fortran (Replies: 2)