Using strcmp() after using strtok()

Discussion in 'C Programming' started by ern, Sep 21, 2005.

  1. ern

    ern Guest

    //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!
    */
     
    ern, Sep 21, 2005
    #1
    1. Advertising

  2. ern wrote:
    > //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
     
    David Resnick, Sep 21, 2005
    #2
    1. Advertising

  3. ern

    Guest

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

    g_UserCommands = strtok(NULL, " ");

    -Ganesh
     
    , Sep 22, 2005
    #3
  4. ern

    Default User Guest

    wrote:

    > Replace below by a strcpy between source and destination and then you
    > will see the intended results
    >
    > g_UserCommands = strtok(NULL, " ");



    See sig below


    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Sep 22, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shane Peck

    strcmp problem

    Shane Peck, Sep 19, 2003, in forum: C++
    Replies:
    6
    Views:
    2,383
    Kevin Goodsell
    Sep 22, 2003
  2. muser

    strcmp

    muser, Oct 3, 2003, in forum: C++
    Replies:
    6
    Views:
    1,156
    Frank Schmitt
    Oct 9, 2003
  3. Andrej Hocevar

    please help with strcmp()

    Andrej Hocevar, Jul 19, 2003, in forum: C Programming
    Replies:
    3
    Views:
    349
    Gordon Burditt
    Jul 19, 2003
  4. collinm

    strtok problem - strcmp

    collinm, Mar 24, 2005, in forum: C Programming
    Replies:
    4
    Views:
    825
    Mark McIntyre
    Mar 24, 2005
  5. Replies:
    10
    Views:
    1,223
    CBFalconer
    Nov 20, 2007
Loading...

Share This Page