I have a vector of strings which was created from parsing a config file. All the strings should be in the format key=value
. I'd like to iterate over the vector, and use the putenv function to set an environment variable to the key-value pair.
The code:
for(auto it = settings.begin(); it != settings.end(); it++) {
try {
auto i = it - settings.begin();
cout << i << endl;
putenv(settings.at(i));
} catch (...) {
cout << "Config is not in the format key=value ... please correct" << endl;
}
}
This throws the error:
cannot convert ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ to ‘char*’ for argument ‘1’ to ‘int putenv(char*)’
I'm very new to C++, and all these variable types and pointers are confusing me.