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. Advertisements

  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. Advertisements

  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. Advertisements

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. collinm

    strtok problem - strcmp

    collinm, Mar 24, 2005, in forum: C Programming
    Replies:
    4
    Views:
    972
    Mark McIntyre
    Mar 24, 2005
  2. Replies:
    20
    Views:
    3,693
    Ben Bacarisse
    Feb 18, 2006
  3. BGP
    Replies:
    12
    Views:
    803
    Default User
    Jun 21, 2005
  4. Replies:
    5
    Views:
    527
  5. wreckingcru
    Replies:
    11
    Views:
    1,430
    red floyd
    Feb 1, 2006
  6. Mr John FO Evans

    using strtok

    Mr John FO Evans, Mar 10, 2007, in forum: C Programming
    Replies:
    14
    Views:
    1,311
    Al Balmer
    Mar 13, 2007
  7. Replies:
    10
    Views:
    1,457
    CBFalconer
    Nov 20, 2007
  8. krista
    Replies:
    5
    Views:
    684
    Jim Langston
    Jan 24, 2008
Loading...