Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Environment Variables In C++

I'd like to have access to the $HOME environment variable in a C++ program that I'm writing. If I were writing code in C, I'd just use the getenv() function, but I was wondering if there was a better way to do it. Here's the code that I have so far:

std::string get_env_var( std::string const & key ) {                                      char * val;                                                                             val = getenv( key.c_str() );                                                            std::string retval = "";                                                                if (val != NULL) {                                                                          retval = val;                                                                         }                                                                                       return retval;                                                                         }            

Should I use getenv() to access environment variables in C++? Are there any problems that I'm likely to run into that I can avoid with a little bit of knowledge?

like image 814
James Thompson Avatar asked Mar 10 '09 18:03

James Thompson


People also ask

How do I access environment variables?

Do so by pressing the Windows and R key on your keyboard at the same time. Type sysdm. cpl into the input field and hit Enter or press Ok. In the new window that opens, click on the Advanced tab and afterwards on the Environment Variables button in the bottom right of the window.

Where are environment variables stored in C?

Environment variables are stored together with command line arguments at the top of the process memory layout, above the stack.

How does Getenv work in C?

The getenv() function searches the environment variables at runtime for an entry with the specified name, and returns a pointer to the variable's value. If there is no environment variable with the specified name, getenv() returns a null pointer.

What is the command to see environment variables?

To display the values of environment variables, use the printenv command. If you specify the Name parameter, the system only prints the value associated with the variable you requested.


2 Answers

There is nothing wrong with using getenv() in C++. It is defined by stdlib.h, or if you prefer the standard library implementation, you can include cstdlib and access the function via the std:: namespace (i.e., std::getenv()). Absolutely nothing wrong with this. In fact, if you are concerned about portability, either of these two versions is preferred.

If you are not concerned about portability and you are using managed C++, you can use the .NET equivalent - System::Environment::GetEnvironmentVariable(). If you want the non-.NET equivalent for Windows, you can simply use the GetEnvironmentVariable() Win32 function.

like image 128
Matt Davis Avatar answered Oct 15 '22 20:10

Matt Davis


I would just refactor the code a little bit:

std::string getEnvVar( std::string const & key ) const {     char * val = getenv( key.c_str() );     return val == NULL ? std::string("") : std::string(val); } 
like image 43
Vlad Avatar answered Oct 15 '22 21:10

Vlad