1

I am using Fortran to do calculation on huge data set which was split into many files. The names of the files are:

maltoLyo12per-reimage-set1.traj
maltoLyo12per-reimage-set2.traj
maltoLyo12per-reimage-set3.traj

The code I wrote to do the calculation is as below:

fileLoop: do j = 31, 34

 OPEN(unit=31,status='old',file=fileplace//'maltoLyo12per-reimage-set1.traj')
 OPEN(unit=32,status='old',file=fileplace//'maltoLyo12per-reimage-set2.traj')
 OPEN(unit=33,status='old',file=fileplace//'maltoLyo12per-reimage-set3.traj')
 OPEN(unit=34,status='old',file=fileplace//'maltoLyo12per-reimage-set4.traj')

 ... operation....

close (j)
end do fileLoop

During the run I want the code to open each file at a time and close them after finish calculation. But the above code will open all the files at once and close them one after one upon finish calculation.

So I tried to alter the code something like below:

fileLoop: do j = 31, 34

 OPEN(unit=j,status='old',file=fileplace//'maltoLyo12per-reimage-set1.traj')

close (j)
end do fileLoop

But here I am facing a problem with the file name. Each time the loop run, the file name doesn't change because of the phrase "set1" in the file name. I want the number in the file name to change like set1, set2, set3, etc., subsequently with file unit number 31,32,33,34, etc.

milancurcic
  • 6,202
  • 2
  • 34
  • 47
Vijay
  • 965
  • 5
  • 13
  • 27
  • 1
    It looks like you would be interested in this question: http://stackoverflow.com/questions/1262695/converting-integers-to-strings-in-fortran. – milancurcic Apr 30 '13 at 03:45

1 Answers1

8

Something like this: (edited to have unit numbers 31 to 34, filenames 1 to 4.)

character (len=90) :: filename

fileLoop: do j = 31, 34

 write (filename, '( "maltoLyo12per-reimage-set", I1, ".traj" )' )  j - 30
 OPEN(unit=j,status='old',file=filename)

close (j)
end do fileLoop
M. S. B.
  • 28,968
  • 2
  • 46
  • 73
  • 1
    While the unit numbers are 31 to 34, the filenames are "set1", ... "set4", so the write should be "write () j-30. – M. S. B. Apr 30 '13 at 05:58
  • Thank you for the help. But I am getting error if the number in set go beyond 9. For example, if the number become "set10", the program stop running. How to fix this error. You help is much appreciated. – Vijay Apr 30 '13 at 10:31
  • If I use I2, I get file names such maltoLyo12per-reimage-set10.traj, maltoLyo12per-reimage-set 9.traj. The is a empty space between "set" and ".traj". Is there anyway to fix this? – Vijay Apr 30 '13 at 10:42
  • You could use I2.2, which will always output two digits, adding a leading zero if necessary. – M. S. B. Apr 30 '13 at 14:05
  • 1
    Or you could use `i*` which will always produce a field wide enough for all the digits in the integer, no more and no less. This approach might disappoint you when using file-system utilities for sorting file names if you want `file2.xtn` to precede `file10.xtn`. – High Performance Mark Apr 30 '13 at 17:28
  • Dear M.S.B, I replaced the "I2" with "I*". But I get error message as follow: (Error: Nonnegative width required in format string at (1) ). By the way, if I use "I2", I get "set 1", "set 2", .... "set10" in the file name. There is a empty space after the word "set" for single digit numbers. – Vijay May 01 '13 at 05:59
  • To always obtain two digits of output, even if the number is less than 10, use "I2.2". If the number doesn't fill the field, leading zeros will be used instead of leading blanks. To obtain the exactly the required number of digits without any leading blanks, use "I0". – M. S. B. May 01 '13 at 06:11
  • Thank you IRO-bot. I could follow the link as you given to achieve my need. Regards – Vijay May 01 '13 at 07:08