This shell script
#!/bin/csh
set VAR=12345
echo $VAR
will peacefully give the output 12345 at shell. I need to use C++ to do the same in some part of the code:
string str = "12345";
retValue="set var1= "+str;
system(retValue1.c_str());
system("echo $var1");
This doesn't create a system variable var1 and echos null which is understandable as each system function would create a child process with different environment variables. So I combine them as follows using just one system function...but it echos null again.
retValue="set var1= "+str;
retValue1=retValue+";\n echo $var1";
system(retValue1.c_str());
Can someone please guide me to set up the system variable thru C++. Thanks a lot in advance!
Look at setenv in <cstdlib>
:
#include <cstdlib>
setenv("VAR", "12345", true);
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