getting free unit number in fortran

三世轮回 提交于 2019-11-28 11:04:28
Jonathan Dursi

In fortran 2008, there's a newunit clause to open that you can use

   integer :: myunit

   ..
   open(newunit=myunit,file='file.dat')
   ...
   close(myunit)

but that's new enough that not all compilers support it yet. If yours doesn't yet, you can mock one up yourself; there's a good example on the fortran wiki.

john

You can use INQUIRE to find a unit number that is not in use:

      integer*4 function get_file_unit (lu_max)
!
!   get_file_unit returns a unit number that is not in use
      integer*4 lu_max,  lu, m, iostat
      logical   opened
!
      m = lu_max  ;  if (m < 1) m = 97
      do lu = m,1,-1
         inquire (unit=lu, opened=opened, iostat=iostat)
         if (iostat.ne.0) cycle
         if (.not.opened) exit
      end do
!
      get_file_unit = lu
      return
      end function get_file_unit
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!