Possible Duplicate:
How to read Linux environment variables in c++
How can the following be changed to do what it's supposed to do?
string s = getenv("PATH");
std::string getEnvVar(std::string const& key)
{
char const* val = getenv(key.c_str());
return val == NULL ? std::string() : std::string(val);
}
You have to check that the getenv
succeeded first:
char const* tmp = getenv( "PATH" );
if ( tmp == NULL ) {
// Big problem...
} else {
std::string s( tmp );
// ...
}
(Supposing I've guessed correctly with regards to "what it's supposed to do".)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With