Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A pseudo-array of output files

Status
Not open for further replies.

JollyJoystickJester

Programmer
Jul 11, 2011
4
US
Hello,
I'm new to this forum. I was wondering if anyone can tell me if it is possible and if they know how to create what I call a pseudo array of output files.

Here is the pseudo code:

Do i=1,15
Do j=1,3
open(unit=(10+i*j),file="i-j.txt",status="new")
EndDo
EndDo

Basically what I am trying to do is create a 15x3 array of files taking up units 11-55. The file names would be "1-1.txt", "2-1.txt",... "15-1.txt", "1-2.txt", "2-2.txt",... "15-2.txt", "1-3.txt", "2-3.txt",... "15-3.txt"

Although my purposes for doing this are probably irrelevent, I'll go over it briefly. I am storing three different "properties" of fifteen different "bodies" over time. There are 5000 time steps and I need a separate file for each property for each body, so I can see the change of each individual property over time.

Any help would be greatly appreciated :)

P.S. I have an idea of what I need to do. I know that I need to somehow convert the integers in the Do loop to strings to make them part of the file name. I unfortunately have no idea how to do this.
 
ry something like :

Code:
CHARACTER(50) filename

Do i=1,15
   Do j=1,3
     write(filename,"(I0,'-',I0,'.txt')") i,j
     open(unit=(10+i*j),file=filename)    
   EndDo
EndDo

The string FILENAME is used as an internal file...


François Jacq
 
Thanks for all your help. I got it working. For all who may need it, this is the exact code I ended up using:

Integer :: i,j,fileindex

Character(len=20) :: filename(45),aa

fileindex = 1
aa = "(I2,A1,I2,A4)"

Do i=1,15
Do j=1,3
write(filename(fileindex),aa) i,"-",j,".txt"
open(unit=(fileindex+10), file=filename(fileindex),
& status="new")
fileindex = fileindex + 1
EndDo
EndDo
 
Few remarks :

- You should use the format I0 instead of I2 because I0 does not insert spaces within the file names. If I0 does not work with your compiler, then use I2.2 instead of I2 (the integer 8, for instance, is written " 8" with I2 and "08" with I2.2).

- Don't create the files with status="new", especially during the test phase. If you launch again your program in forgetting to delete the files created by the previous run, it will crash.


François Jacq
 
I noticed the problem with the spaces. Thanks a lot, it really helped when I changed it. As for the status, what status should I give the file instead?
 
No status mean status='unknown'. It is often what one needs.

status='new' must be used when one wants to stop the code if the file already exists : it is useful to avoid the deletion of a file by mistake.

status='old' must be used when one wants to open file which is supposed to exist already. An option often used when reading a file.

François Jacq
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top