John Dodson
John Dodson

Reputation: 27

using strtok to parse command line args in c++

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

Answers (1)

user2672165
user2672165

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

Related Questions