supertoto
supertoto

Reputation: 29

Fortran WRITE loop to multiple files?

i want to create new file at each loop, i don't know how to do...

!file1, file2,....., file[n]

OPEN (1,FILE='file1.out',ACCESS='SEQUENTIAL',STATUS='UNKNOWN')
do ph=1,N6
do i=1,nx-1
    A(i)=mu*U(i-1)
end do
do j=0,nx
    U(j)=A(j)
end do 
    if (mod(ph,Ne)==0) then ! ?
    WRITE(1,200) nt,U(i)
    endif
    200 format(5E12.4)
end do

Or maybe, i can write with a newline or column ? I'm a beginner in fortran. Thanks

Upvotes: 0

Views: 7123

Answers (1)

M. S. B.
M. S. B.

Reputation: 29381

Your existing code should output more than just last write statement to the file ... it is a sequential file, which means that the output is added to the file in sequential order. If you are only seeing one output, perhaps that is all that the IF statement is causing to be output?

If you still wish to output to multiple files, the easiest way to output to multiple files is to reuse the unit number and have the program create filenames. You need to close the file / unit and reopen it. This is much more easier than have multiple open statements and unit numbers, which would quickly become awkward as the number of files increased. Here is a code fragment that assumes fewer than 100 files:

do i=1, N
  write (filename, '("myfile", I2.2, ".txt")' )  I 
  open (file=filename,unit=16,...)
  calculations...
  write (16,'(5E12.4)') nt,U(i)
  close (16)
end do

Upvotes: 2

Related Questions