I receive from another method a string
(I don’t know the size of this) and I want to fill my argv
(and get argc
) with this string
to pass to other method and I don’t know how to do it.
At the start of the string
I set the name of my app so I have a final string
like:
"myapp arg1 arg2 arg3 arg4"
The code I have is the following:
int main (int argc, const char* argv[])
{
while(true)
{
// send_string() give a string like: “the_name_of_my_app arg1 arg2 arg3 arg4”
std::string data = send_string();
argv = data;
argc = number_of_element_on_data;
other_function(argc, argv);
}
return 0;
}
Try something like this:
#include <vector>
#include <string>
#include <sstream>
int main (int argc, const char* argv[])
{
while (true)
{
// send_string() give a string like: “the_name_of_my_app arg1 arg2 arg3 arg4”
std::string data = send_string();
std::istringstream iss(data);
std::string token;
std::vector<std::string> args;
while (iss >> token)
args.push_back(token);
std::vector<const char*> ptrs(args.size()+1);
for(size_t i = 0; i < args.size(); ++i)
ptrs[i] = args[i].c_str();
ptrs[args.size()] = NULL;
other_function(args.size(), ptrs.data());
}
return 0;
}