簡體   English   中英

在 C++ 中設置本地環境變量

[英]Set local environment variables in C++

如何在 C++ 中設置環境變量?

  • 他們不需要堅持過去的程序執行
  • 它們只需要在當前進程中可見
  • 偏好獨立於平台但我的問題只需要在 Win32/64 上工作

謝謝

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.

在 Win32 上,我相信它被稱為 _putenv。

如果您喜歡又長又丑的函數名,請參閱SetEnvironmentVariable

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

我不是肯定的環境變量是你所需要的,因為它們不會在程序運行之外使用。 無需使用操作系統。

您最好擁有一個單例類或一個包含所有這些值的命名空間,並在您啟動程序時對其進行初始化。

#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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM