Possible duplicate:How to read Linux environment variables in C ++
How can you change the following to do what he was supposed to do?
string s = getenv("PATH");
You should check that getenv first:
getenv
char const* tmp = getenv( "PATH" ); if ( tmp == NULL ) { // Big problem... } else { std::string s( tmp ); // ... }
(Suppose I guessed correctly that this should do. ")
std::string getEnvVar(std::string const& key) { char const* val = getenv(key.c_str()); return val == NULL ? std::string() : std::string(val); }