【问题标题】:Is there a way in Fortran to write to an output file from subroutine when the file has open statement in main program.当文件在主程序中有 open 语句时,Fortran 有没有办法从子程序写入输出文件。
【发布时间】:2013-03-14 06:00:43
【问题描述】:

在 Fortran 中,当文件在主程序中有 open 语句时,我试图从子程序写入输出文件。换句话说,我如何将文件单元号(终端号)从主程序传递给子程序。对此的任何想法都非常感谢。例如,我的代码如下所示,

program main1
open(unit=11,file='output.dat')
call subroutine1
...
call subroutine1
...
end program main1


subroutine subroutine1
write(11,*)'dummy'
...
write(11,*)'dummy'
...
end subroutine subroutine1

【问题讨论】:

    标签: fortran main output subroutine


    【解决方案1】:

    通过传递代表打开文件的整数:

    module mod1
      implicit none
    contains
      subroutine subroutine1(fp)
        integer, intent(in) :: fp
        write(fp,*)'dummy'
        write(fp,*)'dummy'
      end subroutine subroutine1
    end module mod1
    
    program main1
      use mod1
      implicit none
      integer :: fp
      fp = 11
      open(unit=fp,file='output.dat')
      call subroutine1(fp)
      close(fp)
    end program main1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-09
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-09
      • 2012-03-23
      相关资源
      最近更新 更多