I have the following char*
char* config = "username 12345 google.com 225";
While trying to split it by space, I expect the result be a vector<string>
of words contained in the string, BUT I instead get only the first word and nothing more.
Here is the code I use:
istringstream iss_str(config);
string token;
// storage for splitted config data
vector<string> tokens;
// port number is an integer value, hence it should
// be type-cast back to integer
int config_int_port = 0;
while( getline(iss_str, token, ' ') ) // I also have replaced space with \000 but to no avail
{
tokens.push_back(token);
}
The result I get is a vector with size 1 which only contains the first word, username
I also have used the following method, but the result is the same as previous one:
copy(istream_iterator<string>(iss_str),
istream_iterator<string>(),
back_inserter(tokens));
UPDATE I use the following function to execute the above code:
void __cmd_conf(char* __config)
And it is called:
__cmd_conf(optarg);
optarg
is linux's global variable for the next option's arguments.