I'm trying to convert a string vector to a char array in c++.
More specifically what I'm trying to do is to split a shell command like "ls –latr" by using this:
istringstream f(x);
while (getline(f, x, ' '))
{
strings.push_back(x);
}
I believe that will give me strings[0] == "ls" and strings[1]==" -latr".
I'm trying then to do the following:
execvp(strings[0], strings);
however, I get this error:
error: cannot convert ‘std::basic_string, std::allocator >’ to ‘const char*’ for argument ‘1’ to ‘int execvp(const char*, char* const*)’
Therefore, I'm trying to figure out how I can convert the strings to a char array.
.c_str()to the end ofstrings[0]; you'll still have to build another vector of pointers for the second parameter though.std::vector<std::string>, usestrings[0].c_str(). the second paramater needs an array of pointers, so that's more complex.while (f >> x) { strings.push_back(x); }.