在c++中设置本地环境变量。

时间:2021-05-23 22:59:01

How do I set an environment variable in C++?

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

  • They do not need to persist past program execution
  • 它们不需要坚持过去的程序执行
  • They only need to be visible in the current process
  • 它们只需要在当前进程中可见。
  • Preference for platform independent but for my problem only needs to work on Win32/64
  • 喜欢平*立,但对于我的问题,只需要在Win32/64上工作

Thanks

谢谢

3 个解决方案

#1


47  

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.

On Win32 it's called _putenv I believe.

在Win32上,它叫做_putenv。

See SetEnvironmentVariable also if you're a fan of long and ugly function names.

如果你喜欢长而难看的函数名,也可以查看SetEnvironmentVariable。

#2


3  

I'm not positive environment variables are what you need, since they aren't going to be used outside of this run of the program. No need to engage the OS.

我不是你需要的正环境变量,因为它们不会在程序运行之外使用。没有必要参与操作系统。

You might be better off having a singleton class or a namespace that holds all these values, and initialize them when you start the program.

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

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

#1


47  

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.

On Win32 it's called _putenv I believe.

在Win32上,它叫做_putenv。

See SetEnvironmentVariable also if you're a fan of long and ugly function names.

如果你喜欢长而难看的函数名,也可以查看SetEnvironmentVariable。

#2


3  

I'm not positive environment variables are what you need, since they aren't going to be used outside of this run of the program. No need to engage the OS.

我不是你需要的正环境变量,因为它们不会在程序运行之外使用。没有必要参与操作系统。

You might be better off having a singleton class or a namespace that holds all these values, and initialize them when you start the program.

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

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