getenv (C/C++), getProperty (Java)

[機能]

  環境テーブルから環境変数の値を取り出します

  Java の場合は,System クラスの static メソッド getProperty を使用します.

[形式]
( C/C++ の場合)

	#include <stdlib.h>

	char *getenv(const char *name)
		name : 環境変数の名前

( Java の場合: System クラスの static メソッド)

	String getProperty(String name)
		
[使用例]

  1. 環境変数 BAS に値を設定します(C/C++)
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	int sw;
    	char *str;
    
    	sw  = putenv("BAS=c:\\basic");
    	str = getenv("BAS");
    
    	printf("sw %d 環境変数 BAS の値は %s\n", sw, str);
    
    	return 0;
    }
    
    (出力)
    
    sw 0 環境変数 BAS の値は c:\basic
    			
  2. 環境変数 BAS に値を設定します(Java)
    import java.io.*;
    
    public class Test {
    
    	public static void main(String args[]) throws IOException
    	{
    		String sw, str;
    
    		sw  = System.setProperty("BAS", "c:\\basic");
    		str = System.getProperty("BAS");
    
     		System.out.println("sw " + sw + " 環境変数 BAS の値は " + str);
    	}
    }
    
    (出力)
    
    sw null 環境変数 BAS の値は c:\basic
    			
[参照]

putenv

ホームページ 目次 演習解答例目次 付録目次 索引