Porblem with strtok

This is a discussion on Porblem with strtok within the C Programming forums, part of the General Programming Boards category; Hi, I'm using strtok to parse the a string using '|' as the delimiter...It worked fine as long as none ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    Porblem with strtok

    Hi,

    I'm using strtok to parse the a string using '|' as the delimiter...It worked fine as long as none of the fields were empty, i.e. text1|text2|text3 worked, but text1||text3 did not. It skipped text2 and returned text3.

    could anyone please help me out with this problem..is ther any way to solve it?? how to capture null in field using strtok.?? ur help will be greatly appreciated

    thanks in advance

    pokks.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    Quote Originally Posted by man strtok
    A `token' is a nonempty string of characters not occurring in the string delim, followed by \0 or by a character occurring in delim.
    This should explain why it will not work with strtok().

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    22
    Quote Originally Posted by pokks
    Hi,

    I'm using strtok to parse the a string using '|' as the delimiter...It worked fine as long as none of the fields were empty, i.e. text1|text2|text3 worked, but text1||text3 did not. It skipped text2 and returned text3.

    could anyone please help me out with this problem..is ther any way to solve it?? how to capture null in field using strtok.?? ur help will be greatly appreciated

    thanks in advance

    pokks.
    Sounds like it's working as intended ?

    It skipped "text2" because "text2" did not exist, there was no token.

    A ātokenā is a nonempty string of characters not occurring in the
    string delim, followed by \0 or by a character occurring in delim.
    If a token ends with a delimiter, this delimiting character is overā
    written with a \0 and a pointer to the next character is saved for the
    next call to strtok().

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    Quote Originally Posted by pokks
    Hi,

    I'm using strtok to parse the a string using '|' as the delimiter...It worked fine as long as none of the fields were empty, i.e. text1|text2|text3 worked, but text1||text3 did not. It skipped text2 and returned text3.

    could anyone please help me out with this problem..is ther any way to solve it?? how to capture null in field using strtok.?? ur help will be greatly appreciated

    thanks in advance

    pokks.
    You could write strok2()
    Code:
    char *strtok2 (char *Str, const char *Del)
    {
       static char *OldStr = NULL;
       char        *Ptr;
    
       if (Str == NULL)
          if (OldStr == NULL)
             return NULL;
          else
             Str = OldStr;
    
       if (Str [0] == '\0') return NULL;
    
       Ptr = Str;
       while (*Ptr != '\0' && !IsDelim (*Ptr, Del))
          Ptr++;
       if (*Ptr != '\0') {
          *Ptr = '\0';
          OldStr = Ptr + 1;
          return Str;
       } else {
          OldStr = NULL;
          return Str;
       }
    }

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Might one presume IsDelim to look something like this?
    Code:
    int IsDelim(char Ch, const char *Del)
    {
       for ( ; *Del; ++Del )
       {
          if ( Ch == *Del )
          {
             return 1;
          }
       }
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    Thanks

    THANKS A LOT FOR ALL UR REPLIES!!!

    It really helped me a lot!!

    Thanks again,
    Pokks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 10:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 10:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 08:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 07:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21