当文件在主程序中具有open语句时,Fortran中有没有办法从子例程写入输出文件。

max*_*axm 3 fortran program-entry-point subroutine output

在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
Run Code Online (Sandbox Code Playgroud)

Bál*_*adi 5

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

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
Run Code Online (Sandbox Code Playgroud)