2009-05-22 12 views
33

C++で環境変数を設定するにはどうすればよいですか?C++でローカル環境変数を設定する

  • 彼らは、現在のプロセスでは、プラットフォームに依存しないため
  • 好み見えるようにする必要がありますが、私の問題のためだけのWin32/64
上で動作する必要があり、過去のプログラム実行
  • を永続化する必要はありません。 Win32で

    おかげ

  • 答えて

    45
     
    NAME 
    
         putenv - change or add an environment variable 
    
    SYNOPSIS 
    
         #include &ltstdlib.h> 
    
         int putenv(char *string); 
    
    DESCRIPTION 
         The putenv() function adds or changes the value of environment 
         variables. The argument string is of the form name=value. If name does 
         not already exist in the environment, then string is added to the 
         environment. If name does exist, then the value of name in the 
         environment is changed to value. The string pointed to by string becomes 
         part of the environment, so altering the string changes the environment. 
    

    それは、私は信じて_putenvと呼ばれています。

    長くて醜い機能名のファンでも、SetEnvironmentVariableを参照してください。

    +4

    質問者への注意 - putenvはWin32でもサポートされています。 –

    +19

    適切なC++ヘッダー名を使用できますか? が適切です(ええ、私は知っています...それは私のハングアップです)。 –

    +4

    主なる神が意図しているのはCです。 – alamar

    3

    私は肯定的ではありませんが、プログラムの実行の外で使用されないため、環境変数は必要なものです。 OSに従事する必要はありません。

    シングルトンクラスまたはこれらの値をすべて保持する名前空間を持つ方がよい場合があります。プログラムの起動時に初期化してください。

    +0

    これらは子プロセスだけが見ることができ、putenv()は通常、OSとは全く話す必要がありません。 – RBerteig

    -2
    #include<stdio.h> 
    #include<unistd.h> 
    #include<stdlib.h> 
    #include<string.h> 
        main(int argc,char *argv[]) 
        { 
    
        char *var,*value; 
         if(argc==1||argc>3) 
         { 
         fprintf(stderr,"usage:environ variables \n"); 
         exit(0); 
         } 
        var=argv[1]; 
        value=getenv(var); 
        //--------------------------------------- 
         if(value) 
         { 
         printf("variable %s has value %s \n",var,value); 
         } 
         else 
         printf("variable %s has no value \n",var); 
         //---------------------------------------- 
         if(argc==3) 
         { 
         char *string; 
         value=argv[2]; 
         string=malloc(strlen(var)+strlen(value)+2); 
          if(!string) 
          { 
          fprintf(stderr,"out of memory \n"); 
          exit(1); 
          } 
          strcpy(string,var); 
          strcat(string,"="); 
          strcat(string,value); 
          printf("calling putenv with: %s \n",string); 
          if(putenv(string)!=0) 
          { 
          fprintf(stderr,"putenv failed\n"); 
          free(string); 
          exit(1); 
          } 
             value=getenv(var); 
          if(value) 
           printf("New value of %s is %s \n",var,value); 
          else 
          printf("New value of %s is null??\n",var); 
         }  
         exit(0); 
    
        }//----main 
    
    
    
    
    
    /* commands to execure on linux compile:- $gcc -o myfile myfile.c 
             run:- $./myfile xyz 
                  $./myfile abc 
                  $./myfile pqr 
    */ 
    
    +8

    このコードはどのように質問に答えますか?なぜあなたはこれを私たちと共有しましたか? –

    +0

    答えは、(1)インクルードファイルと(2)1行のコードで構成されていると思います。そして、私がリンクしなければならないライブラリかもしれません。 – notlesh

    +0

    notlesh、なぜですか?また、コンパイルの指示もあります。なぜファイルを追加するのですか?私はこれがなぜ投票されたのか分からない。 – Owl

    関連する問題