在 Fortran 中编写多个输出文件

发布于 2024-11-10 00:03:24 字数 885 浏览 6 评论 0原文

亲爱的大家,我正在编写一个代码,将输出写入多个名为 1.dat、2.dat、..... 这是我的代码,但它给出了一些不寻常的输出。你能告诉我我的代码有什么问题吗?基本上我无法获得正确的语法来打开多个文件,在它们上写入并在打开下一个文件之前关闭。谢谢。 我的代码

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

几度春秋 2024-11-17 00:03:24

因此,有一些关于您的代码的事情还不清楚,但我认为这里最相关的部分是您希望在 open 语句中使用 file = 指定文件名,而不是格式,并且使用 iout 循环单位是有问题的,因为您最终会遇到 stdin 和 stdout 的系统定义单位。另外,使用该格式行看起来您已经准备好创建文件名,但您从未真正使用过它。

我不确定你在哪里;进行 mod 测试等,但下面是上面的精简版本,它仅在循环中创建文件:

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

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
缱倦旧时光 2024-11-17 00:03:24

就我而言,我希望文件名有一个前缀,如dyn_

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文