Writing multiple output files in Fortran

前端 未结 2 2019
孤街浪徒
孤街浪徒 2020-12-04 01:09

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 i

相关标签:
2条回答
  • 2020-12-04 01:20

    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
    
    0 讨论(0)
  • 2020-12-04 01:35

    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
    
    0 讨论(0)
提交回复
热议问题