51

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

  • 他们不需要坚持过去的程序执行
  • 它们只需要在当前进程中可见
  • 偏好平台独立但我的问题只需要在 Win32/64 上工作

谢谢

4

4 回答 4

60
姓名

       putenv - 更改或添加环境变量

概要

       #include <stdlib.h>

       int putenv(char *string);

描述
       putenv() 函数添加或更改环境的值
       变量。参数字符串的格式为 name=value。如果名字有
       环境中尚不存在,则将字符串添加到
       环境。如果 name 确实存在,则 name 中的值
       环境改变为价值。string 指向的字符串变为
       环境的一部分,所以改变字符串会改变环境。

我相信在 Win32 上它被称为 _putenv。

如果您喜欢长而丑陋的函数名称,请参阅SetEnvironmentVariable 。

于 2009-05-22T19:14:18.777 回答
6

还有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

于 2020-05-14T19:46:21.480 回答
3

我不肯定环境变量是您需要的,因为它们不会在程序运行之外使用。无需使用操作系统。

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

于 2009-05-22T19:53:00.807 回答
-1
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int 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
于 2011-04-27T11:25:57.510 回答