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

FORTRAN Open Arbitrary number of files in Do Loop

  1. Apr 17, 2009 #1

    minger

    User Avatar
    Science Advisor

    I have a quick question that I've always wondered exactly how to do but never needed to. Well of course now I do. I have a DO loop that loops an arbitrary number of times. I would like to open a file each time it comes around, using the DO loop iterator as part of the filename. Ideally, it would look something like....
    Code (Text):

    n = 5
    DO i=1,n
     OPEN(11,file='input000',i,'.fmt',form='formatted')
     blah blah blah
     CLOSE(11)
    END DO
     
    So, if anyone knows how to parse the variable into that text string, I would appreciate it. Thanks,
     
  2. jcsd
  3. Apr 17, 2009 #2
    I do not know what version of Fortran you run, but this example should be close to what you can do.

    Code (Text):

          IMPLICIT INTEGER(I-N), REAL*8(A-H,O-Z)
          N=5
          CHARACTER *10 FN
          DO 5 I=1,N
          WRITE(FN,10)I
          WRITE(6,*)FN
          OPEN(1,FILE=FN)
          CLOSE(1)
        5 CONTINUE
       10 FORMAT(5HINPUT,I3.3)
          STOP
          END
     
    The screen output looks like this, and the corresponding files have been opened (and closed):
    Code (Text):

    INPUT001
    INPUT002
    INPUT003
    INPUT004
    INPUT005
     
     
  4. Apr 17, 2009 #3

    minger

    User Avatar
    Science Advisor

    BTW, I'm using FORTRAN90. I get that and it'll get me 90% there. How can I then add a '.fmt' as a file extension? I imagine the period needs some special character designation?

    Thanks,
     
  5. Apr 17, 2009 #4
    You only have to add the suffix at the end of the format statement, and don't forget to inccrease the length of FN to 12, as follows:

    Code (Text):

          IMPLICIT INTEGER(I-N), REAL*8(A-H,O-Z)
          N=5
          CHARACTER *12 FN
          DO 5 I=1,N
          WRITE(FN,10)I
          WRITE(6,*)FN
          OPEN(1,FILE=FN)
          CLOSE(1)
        5 CONTINUE
       10 FORMAT('INPUT',I3.3,'.FMT')
          STOP
          END
     
    I believe the translation to F90 should be trivial.
     
  6. Apr 17, 2009 #5

    minger

    User Avatar
    Science Advisor

    Beautiful; working. Thanks a bunch!
     
  7. Apr 17, 2009 #6
    Glad that everything works out!
     
  8. Jun 4, 2009 #7
    Hi everyone

    I have got similar problem..

    How can i write to an existing file without deleting previous data.

    so basically i want to add new info every time i run the program.


    Thanks
     
  9. Jun 4, 2009 #8

    minger

    User Avatar
    Science Advisor

    Try using
    Code (Text):

    ENDFILE
    http://docs.hp.com/en/B3908-90002/ch08s06.html?btnNext=next%A0%BB [Broken]
     
    Last edited by a moderator: May 4, 2017
  10. Jun 4, 2009 #9
    Open the file with the keyword 'append', for example:

    Code (Text):
    OPEN(1, FILE='mydata.dat', ACCESS='APPEND')
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: FORTRAN Open Arbitrary number of files in Do Loop
Loading...