2

Dear All, I am writing a code that writes the out put in multiple files named as 1.dat, 2.dat, ..... Here is my code but it gives some unusual output. May you tell me what is wrong in my code please? Basically I could not get the correct syntax to open multiple files, write on them and close before the next file is opened. Thank you. My Code:

implicit double precision (a-h,o-z),integer(i-n)
dimension b(3300,78805),bb(78805)
character*70,fn 
character*80,fnw 
nf = 3600       ! NUMBER OF FILES
nj = 360        ! Number of rows in file.
do j = 1, nj
    bb(j)  = 0.0
end do
c-------!Body program-----------------------------------------------
iout = 0    ! Output Files upto "ns" no.
DO i= 1,nf  ! LOOP FOR THE NUMBER OF FILES
    if(mod(i,180).eq.0.0) then
        open(unit = iout, file = 'formatted')
        x = 0.0
        do j = 1, nj
            bb(j) = sin(x)
            write(iout,11) int(x),bb(j)
            x = x + 1.0
        end do
        close(iout)
        iout = iout + 1
    end if
END DO
11  format(i0,'.dat')   
END
Jonathan Dursi
  • 50,107
  • 9
  • 127
  • 158
ndphysics
  • 35
  • 1
  • 3
  • What is the modulo supposed to do here? – talonmies May 27 '11 at 12:57
  • If you're writing the code, I strongly encourage you to avoid using implicit variables. This can make the code harder to read, maintain and use in the future — not just by you, but by anyone else. More generally, I suggest using F90 or newer rather than F77. – jvriesem Aug 12 '21 at 16:44

2 Answers2

4

So there are a few things not immediately clear about your code, but I think here the most relevant bits are that you want to specify the filename with file = in the open statement, not the formatting, and looping over units with iout is problematic because you'll eventually hit system-defined units for stdin and stdout. Also, with that format line it looks like you're getting ready to create the filename, but you never actually use it.

I'm not sure where you're; going with the mod test, etc, but below is a stripped down version of above which just creates the files ina loop:

program manyfiles
    implicit none
    character(len=70) :: fn
    integer, parameter :: numfiles=40
    integer, parameter :: outunit=44

    integer :: filenum, j

    do filenum=1,numfiles
        ! build filename -- i.dat
        write(fn,fmt='(i0,a)') filenum, '.dat'

        ! open it with a fixed unit number
        open(unit=outunit,file=fn, form='formatted')

        ! write something
        write(outunit, *) filenum

        ! close it 
        close(outunit)
    enddo
end program manyfiles
Jonathan Dursi
  • 50,107
  • 9
  • 127
  • 158
  • 1
    Great Jonathan. I could implement the idea to solve my problem. Thank you So much. – ndphysics Jun 02 '11 at 14:14
  • Related, but tangential: It's definitely not a good idea to just use IO units haphazardly, as mentioned above. If you use Fortran 2003 or later (actually, it works with F90/95 code too), you can `use, intrinsic :: ISO_FORTRAN_ENV`, which specifically contains the standard I/O units by name rather than using a random number or the counting variable in a `do` loop. – jvriesem Aug 12 '21 at 16:49
0

In my case, I want the file name have an prefix likedyn_

program manyfiles
implicit none
character(len=70) :: filename
integer, parameter :: numfiles=40
integer, parameter :: outunit=44

integer :: filenum, j

do filenum=1,numfiles
    write(filename,'("dyn_",i0,".dat")') filenum
    open(unit=outunit,file=filename, form='formatted')
    write(outunit, *) filenum
    close(outunit)
enddo
end program manyfiles
  • 1
    https://stackoverflow.com/questions/1262695/convert-integers-to-strings-to-create-output-filenames-at-run-time Note that some of the problems mentioned by Jonathan Dursi remain. You are very likely to hit the pre-connected files at (most-likely) units 5 and 6. Personally, I find it much more clean and Fortranny to have all the strings in the output list and not in the format string as they do in C. – Vladimir F Героям слава Mar 26 '19 at 22:03