strtok problem - strcmp

Discussion in 'C Programming' started by collinm, Mar 24, 2005.

  1. collinm

    collinm Guest

    hi

    this is my code to analyse a file

    void analyzeFilename()
    {
    char string[]="B_L2_HLD_GRN_NOR_Run_Counter.txt";
    char *tokenptr;
    char *seperators="_";

    tokenptr = strtok(string,seperators); /* get the first token
    */
    while (tokenptr != NULL) /* while more tokens in
    strng */
    {
    printf("\t%s\n",tokenptr); /* print first token */
    tokenptr = strtok(NULL,seperators); /* get next token */

    }
    }


    that work fine

    now i want to compare the token string, i do:


    void analyzeFilename()
    {
    char string[]="B_L2_HLD_GRN_NOR_Run_Counter.txt";
    char *tokenptr;
    char *seperators="_";

    tokenptr = strtok(string,seperators); /* get the first token
    */
    while (tokenptr != NULL) /* while more tokens in
    strng */
    {
    printf("\t%s\n",tokenptr); /* print first token */
    tokenptr = strtok(NULL,seperators); /* get next token */

    if(strcmp(tokenptr,"HLD")==0)
    printf("hold\n");
    }
    }

    i get a segmentation fault

    any idea?
    collinm, Mar 24, 2005
    #1
    1. Advertising

  2. collinm

    Eric Sosman Guest

    collinm wrote:
    > [...]
    > now i want to compare the token string, i do:
    >
    >
    > void analyzeFilename()
    > {
    > char string[]="B_L2_HLD_GRN_NOR_Run_Counter.txt";
    > char *tokenptr;
    > char *seperators="_";
    >
    > tokenptr = strtok(string,seperators); /* get the first token
    > */
    > while (tokenptr != NULL) /* while more tokens in
    > strng */
    > {
    > printf("\t%s\n",tokenptr); /* print first token */
    > tokenptr = strtok(NULL,seperators); /* get next token */
    >
    > if(strcmp(tokenptr,"HLD")==0)
    > printf("hold\n");
    > }
    > }
    >
    > i get a segmentation fault


    Do the strcmp() *before* strtok().

    --
    Eric Sosman, Mar 24, 2005
    #2
    1. Advertising

  3. collinm

    Michael Mair Guest

    collinm wrote:
    > hi
    >
    > this is my code to analyse a file
    >
    > void analyzeFilename()
    > {
    > char string[]="B_L2_HLD_GRN_NOR_Run_Counter.txt";
    > char *tokenptr;
    > char *seperators="_";
    >
    > tokenptr = strtok(string,seperators); /* get the first token
    > */
    > while (tokenptr != NULL) /* while more tokens in
    > strng */
    > {
    > printf("\t%s\n",tokenptr); /* print first token */
    > tokenptr = strtok(NULL,seperators); /* get next token */
    >
    > }
    > }
    >
    >
    > that work fine
    >
    > now i want to compare the token string, i do:
    >
    >
    > void analyzeFilename()
    > {
    > char string[]="B_L2_HLD_GRN_NOR_Run_Counter.txt";
    > char *tokenptr;
    > char *seperators="_";
    >
    > tokenptr = strtok(string,seperators); /* get the first token
    > */
    > while (tokenptr != NULL) /* while more tokens in
    > strng */
    > {
    > printf("\t%s\n",tokenptr); /* print first token */
    > tokenptr = strtok(NULL,seperators); /* get next token */


    tokenptr will be NULL in the very last loop iteration at this
    point; thus, using it after this line will give you undefined
    behaviour (and in your case, luckily, a segfault).
    Put the call to strtok() at the end of the loop.

    I have not looked to closely at your code nor tested it.

    --Michael
    >
    > if(strcmp(tokenptr,"HLD")==0)
    > printf("hold\n");
    > }
    > }
    >
    > i get a segmentation fault
    >
    > any idea?
    >



    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Mar 24, 2005
    #3
  4. collinm

    John Valko Guest

    collinm wrote:
    > void analyzeFilename()
    > {
    > char string[]="B_L2_HLD_GRN_NOR_Run_Counter.txt";
    > char *tokenptr;
    > char *seperators="_";
    >
    > tokenptr = strtok(string,seperators); /* get the first token
    > */
    > while (tokenptr != NULL) /* while more tokens in
    > strng */
    > {
    > printf("\t%s\n",tokenptr); /* print first token */
    > tokenptr = strtok(NULL,seperators); /* get next token */
    >

    Consider the condition that breaks your loop -- when strtok returns
    NULL. But before that condition is reached, you use the returned value
    without checking it. My guess is that it's returning NULL and then
    you're passing the NULL to strcmp() which causes the segmentation fault.

    > if(strcmp(tokenptr,"HLD")==0)
    > printf("hold\n");
    > }
    > }
    >
    > i get a segmentation fault
    >
    > any idea?
    >


    hope that helps,
    John
    John Valko, Mar 24, 2005
    #4
  5. On 24 Mar 2005 09:22:36 -0800, in comp.lang.c , "collinm"
    <> wrote:

    > tokenptr = strtok(NULL,seperators); /* get next token */


    if strtok returned NULL here...
    >
    > if(strcmp(tokenptr,"HLD")==0)


    .....this will segfault

    you need to check for NULL before using the pointer
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
    Mark McIntyre, Mar 24, 2005
    #5
    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,340
    Kevin Goodsell
    Sep 22, 2003
  2. muser

    strcmp

    muser, Oct 3, 2003, in forum: C++
    Replies:
    6
    Views:
    1,099
    Frank Schmitt
    Oct 9, 2003
  3. ern

    Using strcmp() after using strtok()

    ern, Sep 21, 2005, in forum: C Programming
    Replies:
    3
    Views:
    904
    Default User
    Sep 22, 2005
  4. rabbits77

    weird problem with strcmp()

    rabbits77, Apr 1, 2010, in forum: C Programming
    Replies:
    20
    Views:
    869
  5. Chad

    Re: Strange strcmp() problem

    Chad, May 15, 2010, in forum: C Programming
    Replies:
    8
    Views:
    345
    Tim Rentsch
    Jun 18, 2010
Loading...

Share This Page