在C ++中设置本地环境变量

时间:2009-05-22 19:12:14

标签: c++ c manpage

如何在C ++中设置环境变量?

  • 他们不需要坚持执行程序
  • 他们只需要在当前流程中可见
  • 对平台无关的偏好,但对于我的问题,只需要在Win32 / 64上工作

由于

4 个答案:

答案 0 :(得分:51)

NAME

       putenv - change or add an environment variable

SYNOPSIS

       #include <stdlib.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.

在Win32上,我相信它叫做_putenv。

如果您是长而丑陋的函数名称的粉丝,请参阅SetEnvironmentVariable

答案 1 :(得分:3)

我不是积极的环境变量是你需要的,因为它们不会在这个程序运行之外使用。无需参与操作系统。

你可能最好拥有一个包含所有这些值的单例类或命名空间,并在启动程序时初始化它们。

答案 2 :(得分:0)

还有setenv,它比putenv稍微灵活一点,因为setenv检查是否已设置环境变量,如果已设置,则不会覆盖它“ overwrite”参数表示您不想覆盖它,并且名称和值是setenv的单独参数:

NAME
        setenv - change or add an environment variable
SYNOPSIS
       #include <stdlib.h>

       int setenv(const char *name, const char *value, int overwrite);

       int unsetenv(const char *name);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       setenv(), unsetenv():
           _POSIX_C_SOURCE >= 200112L
               || /* Glibc versions <= 2.19: */ _BSD_SOURCE
DESCRIPTION
       The setenv() function adds the variable name to the environment with
       the value value, if name does not already exist.  If name does exist
       in the environment, then its value is changed to value if overwrite
       is nonzero; if overwrite is zero, then the value of name is not
       changed (and setenv() returns a success status).  This function makes
       copies of the strings pointed to by name and value (by contrast with
       putenv(3)).

       The unsetenv() function deletes the variable name from the
       environment.  If name does not exist in the environment, then the
       function succeeds, and the environment is unchanged.

我并不是说一个比另一个更好或更差;这取决于您的应用程序。

请参见http://man7.org/linux/man-pages/man3/setenv.3.html

答案 3 :(得分:-1)

#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
*/