FORTRAN Open Arbitrary number of files in Do Loop

In summary: This will allow you to write to the end of an existing file without deleting the previous data. You can also specify the position where you want to append the new data using the keyword 'POSITION', for example:OPEN(1, FILE='mydata.dat', ACCESS='APPEND', POSITION='APPEND') This will append the new data to the end of the existing file.
  • #1
minger
Science Advisor
1,496
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
  • #2
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
 
  • #3
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,
 
  • #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:
      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.
 
  • #5
Beautiful; working. Thanks a bunch!
 
  • #6
Glad that everything works out!
 
  • #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
 
  • #8
Try using
Code:
ENDFILE
http://docs.hp.com/en/B3908-90002/ch08s06.html?btnNext=next%A0%BB [Broken]
 
Last edited by a moderator:
  • #9
Open the file with the keyword 'append', for example:

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

1. What is FORTRAN?

FORTRAN (Formula Translation) is a high-level programming language used primarily for scientific and engineering computations. It was developed in the 1950s and has undergone several updates since then.

2. How do I open an arbitrary number of files in a DO loop in FORTRAN?

In FORTRAN, you can use the OPEN statement along with a DO loop to open multiple files. You will need to use the FILE keyword and specify the unit number for each file you want to open. You can also use the STATUS keyword to check for errors while opening the files.

3. Is it possible to open files dynamically in a FORTRAN DO loop?

Yes, it is possible to open files dynamically in a DO loop in FORTRAN. You can use the INQUIRE statement to get the number of files you need to open, and then use a DO loop to open each file with a different unit number.

4. How can I close all the files opened in a FORTRAN DO loop?

To close all the files opened in a DO loop in FORTRAN, you can use the CLOSE statement with a DO loop. You will need to specify the unit numbers of the files you want to close. This will ensure that all the files are closed properly and any remaining data is written before the program ends.

5. Are there any limitations to the number of files I can open in a FORTRAN DO loop?

Yes, there are limitations to the number of files you can open in a DO loop in FORTRAN. The maximum number of files that can be opened simultaneously depends on the system you are using and the version of FORTRAN. In most cases, the limit is around 1000 files. It is recommended to check the documentation of your FORTRAN compiler for the exact limit.

Similar threads

  • Programming and Computer Science
Replies
12
Views
7K
  • Programming and Computer Science
Replies
1
Views
418
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
12
Views
993
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
5
Views
966
  • Programming and Computer Science
Replies
1
Views
3K
Back
Top