#include <iostream> #include <cstdlib> #include <string> using namespace std; #pragma warning(disable: 4996) // disable warning for getenv() int main() { char *pointer = NULL; string str; cout<<"Enter the environment variable: "; while (cin>>str) { if(str.compare("exit") == 0) break; if(pointer = getenv(str.c_str())) { cout<<endl<<"Value of \""<<str<<"\" Variable: "<<pointer<<endl<<endl<<"Enter the environment variable: "; }else { cout<<"No such variables defined."<<endl<<"Enter the environment variable: "; } } }
Wednesday, October 31, 2012
Query system for an environment variable value using C/C++
Today one of my colleague asked me how to query an environment variable using C++. The solution is simple, just use getenv() method. See the below code.
Labels:
c,
c++,
environment,
getenv