在Fortran中编写多个输出文件 [英] Writing multiple output files in Fortran

查看:263
本文介绍了在Fortran中编写多个输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 隐式双精度(ah,oz),整数(英寸)
尺寸b(3300,78805),bb(78805)
字符* 70,fn
字符* 80,fnw
nf = 3600!文件数量
nj = 360!文件中的行数。
do j = 1,nj
bb(j)= 0.0
end do
c -------!正文程式--------- --------------------------------------
iout = 0!输出文件高达nsno。
DO i = 1,nf! LOOP FOR文件数量
if(mod(i,180).eq.0.0)然后
open(unit = iout,file ='formatted')
x = 0.0
(j)= bb(j)
x = x + 1.0
(iout,11)int(x),j = 1,nj
bb (iout)
iout = iout + 1
end if
END DO
11格式(i0,'。dat')
结束


解决方案

代码,但我认为这里最相关的位是你想在打开中指定文件名 file = 声明,而不是格式化,并且使用iout循环单元是有问题的,因为您最终会为stdin和stdout命中系统定义的单元。此外,使用该格式的行,您似乎已准备好创建文件名,但您从未真正使用它。



我不确定您的位置;使用mod测试等,但下面是一个精简版的上面,它只是在循环中创建文件:

 程序manyfiles 
隐式无
字符(len = 70):: fn
整数,参数:: numfiles = 40
整数,参数:: outunit = 44

integer :: filenum,j

做filenum = 1,numfiles
!建立文件名 - i.dat
写(fn,fmt ='(i0,a)')filenum,'.dat'

!打开一个固定的单元号
open(unit = outunit,file = fn,form ='formatted')

!写一些
写(outunit,*)filenum

!关闭它
关闭(outunit)
enddo
结束程序manyfiles


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

解决方案

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

这篇关于在Fortran中编写多个输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆