【问题标题】:getting free unit number in fortran在 fortran 中获取免费单元号
【发布时间】:2011-10-24 13:07:05
【问题描述】:

我需要开发一个可以打开文件并解析内容的库。 单元号,由于fortran IO风格,必须由我决定,但我不知道客户端代码中还有哪些单元是开放的。有没有像give_me_any_unit_number_that_is_free()这样的标准函数?

【问题讨论】:

    标签: fortran fortran95


    【解决方案1】:

    在 fortran 2008 中,有一个可以打开的 newunit 子句供您使用

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

    但这已经足够新了,并不是所有的编译器都支持它。如果你还没有,你可以自己模拟一个; fortran wiki 上有一个很好的例子。

    【讨论】:

      【解决方案2】:

      您可以使用 INQUIRE 查找未使用的单元号:

            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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-10
        • 1970-01-01
        • 2017-10-14
        • 1970-01-01
        • 1970-01-01
        • 2021-05-19
        • 2014-11-19
        相关资源
        最近更新 更多