Reputation: 27
I'm trying to parse command line args with strtok. I get one successful token before I enter a loop but once I enter my while loop to tokenize the rest of my args, it quits working Any ideas? code:
int main(int argc, char *argv[])
{
char *t = NULL;
t = strtok(*argv," ,.-");
while (t != NULL)
{
cout << t << endl;
t = strtok (NULL, " ,.-");
}
return 0;
}
Upvotes: 0
Views: 1775
Reputation: 3049
This is not the way strtok works. Subsequent calls to strtok will continue to process the first argument that was supplied in the first call. strtok shall not be used for this as the arguments already have been tokenized, i.e. they are not in one single char array.
Upvotes: 1