Using strcmp() after using strtok()

E

ern

//I used strtok() like this:

fgets(userCommand, MAX_COMMAND_SIZE, stdin);
g_UserCommands[0] = strtok(command, " ");
while(g_UserCommands != NULL){
i+=1;
g_UserCommands = strtok(NULL, " ");
}

//Now I want to use strcmp() like this:

if(strcmp(g_UserCommands[0], "script")==0){
return (executeScript(arg1));
}

/* Unfortuantely, I'm not getting desired results, probably because
the strings in g_UserCommands are not null terminated. Any suggestions
on a better way to get null terminated strings in g_UserCommands or
suggestions in general? I was hoping for a function that does what
strtok() does, but also providing null termination.

Thanks!
*/
 
D

David Resnick

ern said:
//I used strtok() like this:

fgets(userCommand, MAX_COMMAND_SIZE, stdin);
g_UserCommands[0] = strtok(command, " ");
while(g_UserCommands != NULL){
i+=1;
g_UserCommands = strtok(NULL, " ");
}

//Now I want to use strcmp() like this:

if(strcmp(g_UserCommands[0], "script")==0){
return (executeScript(arg1));
}

/* Unfortuantely, I'm not getting desired results, probably because
the strings in g_UserCommands are not null terminated. Any suggestions
on a better way to get null terminated strings in g_UserCommands or
suggestions in general? I was hoping for a function that does what
strtok() does, but also providing null termination.

Thanks!
*/


Is that the real code? If so, you are reading into the
userCommand variable and strtoking the command variable. Can
you show a complete small compilable program that shows
your problem?

strtok does terminate the strings, that is not your problem.

-David
 
G

ganesh.gella

Replace below by a strcpy between source and destination and then you
will see the intended results

g_UserCommands = strtok(NULL, " ");

-Ganesh
 
D

Default User

Replace below by a strcpy between source and destination and then you
will see the intended results

g_UserCommands = strtok(NULL, " ");



See sig below
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top