Domanda

Come faccio a impostare una variabile di ambiente in C ++?

  • Non c'è bisogno di persistere l'esecuzione del programma passato
  • Hanno solo bisogno di essere visibile nel processo in corso
  • Preferenze per la piattaforma indipendente, ma per il mio problema ha solo bisogno di lavorare su Win32 / 64

Grazie

È stato utile?

Soluzione

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.

Il Win32 si chiama _putenv credo.

SetEnvironmentVariable anche se sei un fan di lunga e nomi brutto funzione.

Altri suggerimenti

Non sono variabili d'ambiente positive sono quello che ti serve, dal momento che non stanno per essere utilizzato al di fuori di questa esecuzione del programma. Non c'è bisogno di coinvolgere il sistema operativo.

Si potrebbe essere meglio avere una classe Singleton o di uno spazio dei nomi che contiene tutti questi valori, e inizializzare quando si avvia il programma.

#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
*/
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top