FORTRAN Open Arbitrary number of files in Do Loop

  • Context: Fortran 
  • Thread starter Thread starter minger
  • Start date Start date
  • Tags Tags
    files Fortran Loop
Click For Summary

Discussion Overview

The discussion revolves around how to open an arbitrary number of files in a DO loop using FORTRAN, specifically focusing on constructing filenames dynamically based on the loop iterator. Participants also address related issues such as appending data to existing files without overwriting previous content.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant seeks guidance on how to construct filenames within a DO loop using the loop iterator.
  • Another participant provides a sample code snippet that demonstrates how to format the filename using a WRITE statement.
  • A participant clarifies that they are using FORTRAN90 and inquires about adding a file extension to the filename.
  • Further clarification is provided on how to include the file extension in the filename format and the necessary adjustments to the character length.
  • A new participant raises a question about writing to an existing file without deleting previous data.
  • Responses suggest using the ENDFILE statement and opening the file with the 'append' access option to achieve this goal.

Areas of Agreement / Disagreement

Participants generally agree on the methods for constructing filenames and appending data to files, but there are multiple approaches suggested for handling file access and formatting, indicating a lack of consensus on the best method.

Contextual Notes

Some limitations include the dependence on specific FORTRAN versions and the need for adjustments in character lengths for filenames, which may vary based on individual implementations.

Who May Find This Useful

This discussion may be useful for programmers working with FORTRAN, particularly those looking to manage file I/O operations within loops or needing to append data to existing files.

minger
Science Advisor
Messages
1,494
Reaction score
2
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:
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,
 
Technology news on Phys.org
I do not know what version of Fortran you run, but this example should be close to what you can do.

Code:
      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:
INPUT001
INPUT002
INPUT003
INPUT004
INPUT005
 
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,
 
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:
      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.
 
Beautiful; working. Thanks a bunch!
 
Glad that everything works out!
 
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
 
Try using
Code:
ENDFILE
http://docs.hp.com/en/B3908-90002/ch08s06.html?btnNext=next%A0%BB
 
Last edited by a moderator:
Open the file with the keyword 'append', for example:

Code:
OPEN(1, FILE='mydata.dat', ACCESS='APPEND')
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
11K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K