How to put getenv () content in a string

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"); 
+7
source share
2 answers

You should check that getenv first:

 char const* tmp = getenv( "PATH" ); if ( tmp == NULL ) { // Big problem... } else { std::string s( tmp ); // ... } 

(Suppose I guessed correctly that this should do. ")

+15
source
 std::string getEnvVar(std::string const& key) { char const* val = getenv(key.c_str()); return val == NULL ? std::string() : std::string(val); } 
+7
source

All Articles