0

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.

Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105
  • 4
    possible dup http://stackoverflow.com/questions/236129/split-a-string-in-c – Sergei Nikulov Sep 08 '15 at 10:18
  • 3
    Can you post the code where you read the tokens vector ? because I tried your code and it works as expected : https://ideone.com/Br1BQO – Vincent Sep 08 '15 at 10:36
  • Another post than may help http://stackoverflow.com/questions/5757721/use-getline-and-while-loop-to-split-a-string. Have you tried printing the value of iss_str before you try to tokenize? You are most likely having issues with the value of iss_str, it seems to me that your code is doing what it should in what concerns the tokenization... – fbastian Sep 08 '15 at 10:44
  • 1
    As a sidenote, assigning string literal to a non-const char pointer has been deprecated since c++03 and illegal since c++11 – eerorika Sep 08 '15 at 10:53
  • Please see the updated question – Mostafa Talebi Sep 08 '15 at 12:06

1 Answers1

1

This code works as expected for me:

#include <string>
#include <sstream>
#include <vector>
#include <iostream>

using namespace std;

int main() {
  const char* config = "username 12345 google.com 225";
  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, ' ') )
    {
      cout << "token " << token << "\n";
      tokens.push_back(token);
    }
  cout <<"tokens len "<<tokens.size()<<"\n";
}

the output is:

token username
token 12345
token google.com
token 225
tokens len 4
Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64