Clicky

Fortran Wiki
newunit (Rev #2, changes)

Skip the Navigation Links | Home Page | All Pages | Recently Revised | Authors | Feeds | Export |

Showing changes from revision #1 to #2: (追記) Added (追記ここまで) | (削除) Removed (削除ここまで) | (削除) Chan (削除ここまで)(追記) ged (追記ここまで)

This is a simple but useful free-unit locator.

(削除) ! This is a simple function to search for an available unit.
! LUN_MIN and LUN_MAX define the range of possible LUNs to check.
! The UNIT value is returned by the function, and also by the optional
! argument. This allows the function to be used directly in an OPEN
! statement, and optionally save the result in a local variable.
! If no units are available, -1 is returned.
integer function newunit(unit)
 integer, intent(out), optional :: unit
! local
 integer, parameter :: LUN_MIN=10, LUN_MAX=1000
 logical :: opened
 integer :: lun
! begin
 newunit=-1
 do lun=LUN_MIN,LUN_MAX
 inquire(unit=lun,opened=opened)
 if (.not. opened) then
 newunit=lun
 exit
 end if
 end do
 if (present(unit)) unit=newunit
end function newunit (削除ここまで)(追記) ! This is a simple function to search for an available unit. (追記ここまで)(追記) 
 (追記ここまで)(追記) ! LUN_MIN and LUN_MAX define the range of possible LUNs to check. (追記ここまで)(追記) 
 (追記ここまで)(追記) ! The UNIT value is returned by the function, and also by the optional (追記ここまで)(追記) 
 (追記ここまで)(追記) ! argument. This allows the function to be used directly in an OPEN (追記ここまで)(追記) 
 (追記ここまで)(追記) ! statement, and optionally save the result in a local variable. (追記ここまで)(追記) 
 (追記ここまで)(追記) ! If no units are available, -1 is returned. (追記ここまで)(追記) 
 (追記ここまで)(追記) integer (追記ここまで)(追記)  (追記ここまで)(追記) function  (追記ここまで)(追記) newunit (追記ここまで)(追記) ( (追記ここまで)(追記) unit (追記ここまで)(追記) ) (追記ここまで)(追記) 
 (追記ここまで)(追記) integer (追記ここまで)(追記) , (追記ここまで)(追記)  (追記ここまで)(追記) intent (追記ここまで)(追記) ( (追記ここまで)(追記) out (追記ここまで)(追記) ), (追記ここまで)(追記)  (追記ここまで)(追記) optional (追記ここまで)(追記)  (追記ここまで)(追記) :: (追記ここまで)(追記)  (追記ここまで)(追記) unit (追記ここまで)(追記) 
 (追記ここまで)(追記) ! local (追記ここまで)(追記) 
 (追記ここまで)(追記) integer (追記ここまで)(追記) , (追記ここまで)(追記)  (追記ここまで)(追記) parameter (追記ここまで)(追記)  (追記ここまで)(追記) :: (追記ここまで)(追記)  (追記ここまで)(追記) LUN_MIN (追記ここまで)(追記) = (追記ここまで)(追記) 10 (追記ここまで)(追記) , (追記ここまで)(追記)  (追記ここまで)(追記) LUN_MAX (追記ここまで)(追記) = (追記ここまで)(追記) 1000 (追記ここまで)(追記) 
 (追記ここまで)(追記) logical (追記ここまで)(追記)  (追記ここまで)(追記) :: (追記ここまで)(追記)  (追記ここまで)(追記) opened (追記ここまで)(追記) 
 (追記ここまで)(追記) integer (追記ここまで)(追記)  (追記ここまで)(追記) :: (追記ここまで)(追記)  (追記ここまで)(追記) lun (追記ここまで)(追記) 
 (追記ここまで)(追記) ! begin (追記ここまで)(追記) 
 (追記ここまで)(追記) newunit (追記ここまで)(追記) =- (追記ここまで)(追記) 1 (追記ここまで)(追記) 
 (追記ここまで)(追記) do (追記ここまで)(追記)  (追記ここまで)(追記) lun (追記ここまで)(追記) = (追記ここまで)(追記) LUN_MIN (追記ここまで)(追記) , (追記ここまで)(追記) LUN_MAX (追記ここまで)(追記) 
 (追記ここまで)(追記) inquire (追記ここまで)(追記) ( (追記ここまで)(追記) unit (追記ここまで)(追記) = (追記ここまで)(追記) lun (追記ここまで)(追記) , (追記ここまで)(追記) opened (追記ここまで)(追記) = (追記ここまで)(追記) opened (追記ここまで)(追記) ) (追記ここまで)(追記) 
 (追記ここまで)(追記) if (追記ここまで)(追記)  (追記ここまで)(追記) (. (追記ここまで)(追記) not. (追記ここまで)(追記)  (追記ここまで)(追記) opened (追記ここまで)(追記) ) (追記ここまで)(追記)  (追記ここまで)(追記) then (追記ここまで)(追記) 
 (追記ここまで)(追記) newunit (追記ここまで)(追記) = (追記ここまで)(追記) lun (追記ここまで)(追記) 
 (追記ここまで)(追記) exit (追記ここまで)(追記) 
 (追記ここまで)(追記) end (追記ここまで)(追記)  (追記ここまで)(追記) if (追記ここまで)(追記) 
 (追記ここまで)(追記) end (追記ここまで)(追記)  (追記ここまで)(追記) do (追記ここまで)(追記) 
 (追記ここまで)(追記) if (追記ここまで)(追記)  (追記ここまで)(追記) ( (追記ここまで)(追記) present (追記ここまで)(追記) ( (追記ここまで)(追記) unit (追記ここまで)(追記) )) (追記ここまで)(追記)  (追記ここまで)(追記) unit (追記ここまで)(追記) = (追記ここまで)(追記) newunit (追記ここまで)(追記) 
 (追記ここまで)(追記) end (追記ここまで)(追記)  (追記ここまで)(追記) function  (追記ここまで)(追記) newunit (追記ここまで)

Here is an example, including the function interface. You can put the above unit into a module, or avoid modules and put the interface into an INCLUDE file. Or, make the argument non-optional and just use an EXTERNAL interface.

(削除) program test_program
 interface
 integer function newunit(unit)
 integer, intent(out), optional :: unit
 end function newunit
 end interface
 integer lun
 open(unit=newunit(lun),file='test')
end program test_program (削除ここまで)(追記) program  (追記ここまで)(追記) test_program (追記ここまで)(追記) 
 (追記ここまで)(追記) interface (追記ここまで)(追記) 
 (追記ここまで)(追記) integer (追記ここまで)(追記)  (追記ここまで)(追記) function  (追記ここまで)(追記) newunit (追記ここまで)(追記) ( (追記ここまで)(追記) unit (追記ここまで)(追記) ) (追記ここまで)(追記) 
 (追記ここまで)(追記) integer (追記ここまで)(追記) , (追記ここまで)(追記)  (追記ここまで)(追記) intent (追記ここまで)(追記) ( (追記ここまで)(追記) out (追記ここまで)(追記) ), (追記ここまで)(追記)  (追記ここまで)(追記) optional (追記ここまで)(追記)  (追記ここまで)(追記) :: (追記ここまで)(追記)  (追記ここまで)(追記) unit (追記ここまで)(追記) 
 (追記ここまで)(追記) end (追記ここまで)(追記)  (追記ここまで)(追記) function  (追記ここまで)(追記) newunit (追記ここまで)(追記) 
 (追記ここまで)(追記) end (追記ここまで)(追記)  (追記ここまで)(追記) interface (追記ここまで)(追記) 
 (追記ここまで)(追記) integer (追記ここまで)(追記)  (追記ここまで)(追記) lun (追記ここまで)(追記) 
 (追記ここまで)(追記) open (追記ここまで)(追記) ( (追記ここまで)(追記) unit (追記ここまで)(追記) = (追記ここまで)(追記) newunit (追記ここまで)(追記) ( (追記ここまで)(追記) lun (追記ここまで)(追記) ), (追記ここまで)(追記) file (追記ここまで)(追記) = (追記ここまで)(追記) 'test' (追記ここまで)(追記) ) (追記ここまで)(追記) 
 (追記ここまで)(追記) end (追記ここまで)(追記)  (追記ここまで)(追記) program  (追記ここまで)(追記) test_program (追記ここまで)
(追記) (追記ここまで)(追記)

category: code

(追記ここまで)
Revision from June 23, 2010 16:39:28 by Joe Krahn
Forward in time (3 more) | Back in time (1 more) | See current | Hide changes | History | Rollback | View: Source | Linked from: Code

AltStyle によって変換されたページ (->オリジナル) /