SO, I am trying to write a function that will return a vector<char**>, as such:
vector<char**> test(string mystr) {
char*temp=new char[mystr.size()+1];
strcpy(temp,mystr.c_str());
char*subStr=strtok(temp,":");
while(subStr!=NULL) {
int i=0;
char**args=new char*[200];
char*tempsta=newchar[strlen(subStr)+1];
strcpy(tempsta, subStr);
args[i]=strtok(tempsta," ");
while(args[i]!=NULL) {
i++;
args[i]=strtok(NULL," ");
}
fullVec.push_back(args);
//cout<<subStr<<endl;
subStr=strtok(NULL,":");
}
return fullVec;
}
so I want to split the parameter string up with ":" delimeter, then with ” ” delimeter. On the cout<<subStr call I get what is expected if I comment out everything from int i=0 to fullVec.push_back(args). If I do not comment out all of those lines I only get the first substring (until the first “:”) is encountered, and then the largest while loop exits.
for what is expected I mean; let us assume the parameter is “my name is: bon jovi: xxx ab”
if everything is commented out, the following lines will be printed:
my name is
bon jovi
xxx ab
if I leave it as is, what will happen is only
my name is
will print, and the large loop will exit
any assistance is appreciated, thanks! (Yes, I am aware that this seems like a silly exercise which can be done much more elegantly/easily…however I would like to get this solution to work before I entertain using string etc.)
Your problem is that strtok() maintains state between calls.
If the first parameter is not NULL then it uses to reset the state otherwise it uses the state it has saved to continue parsing from where it left off.
Since you have two nested calls to strtok() the second call is messing with state of the outer call.
This call:
Is resetting the internal state of strtok(). It now no longer knows anything about the state from your outer call. Thus when you get to the end of the string in your inner loop.
This call:
Is now using the saved state of the inner loop. So it basically just terminates as you have already reached the end of that tokenizing stream.