Fortran Tip集

前へ   上へ   次へ

16 環境変数の値を取得する方法

Fortran 2003 から導入された組込み手続 get_environment_variable を用いて環境変数の値を取得する例を以下に示します。

[ get-environment-variable.f90 ] - 環境変数の値を取得するサンプル

program main
  implicit none
  call show('FRED')
  call show('USER')
  call show('USERNAME')
contains
  subroutine show(name)
    character(*), intent(in) :: name
    character(:), allocatable :: value
    integer len, status
    intrinsic get_environment_variable
    call get_environment_variable(name, status = status, length = len)
    if (status == 1) then
      print *, 'Environment variable "', name, '" does not exist.'
    else if (status /= 0) then
      print *, 'Error', status, 'for environment variable "', name, '"'
    else
      allocate (character(len) :: value)
      call get_environment_variable(name, value = value)
      print *, 'Environment variable "', name, '" has the value "', value, '".'
    end if
  end subroutine
end program



前へ   上へ   次へ
関連情報
ご案内
Privacy Policy  /  Trademarks