Solved

Question re: strtok

Posted on 2004-11-01
267 Views
Last Modified: 2010-04-01
If I have a  string which is in the following format...  
 INFO : dev1
  TYPE :  TYPE4
  STYPE :  typ3

I want to get the value after TYPE which is TYPE4 without the spaces and colon.

 How do I go about doing this using strtok?

0
Question by:jewee
    13 Comments
     
    LVL 17

    Expert Comment

    by:rstaveley
    Pass strtok the separators "\r\n\t :"
    0
     
    LVL 86

    Expert Comment

    by:jkr
    I assume, that the line brealk is intentional. Then, you can use

    char tag [] = "TYPE :";
    char data[] = ...;
    char result[128];

    char* token;

    token = strtok(data,"\n");

    while ( token) {

        if (strstr(token,tag)) { // found "TYPE :"

            strcpy ( result, token + strlen(tag) + 1);

            break;
        }

        token = strtok(NULL, "\n");
    }
    0
     
    LVL 86

    Expert Comment

    by:jkr
    >>"\r\n\t :"

    That would return a lot of tokens...
    0
     
    LVL 17

    Expert Comment

    by:rstaveley
    Yes,

      INFO dev1 TYPE TYPE4 STYPE typ3

    I was assuming those tokens were relevant, and would want to be tested. There's always a bit of mind-reading in EE when the questions are brief :-)
    0
     

    Author Comment

    by:jewee
    with the separators
    \r\n\t...

    it will only return the line:
    TYPE : TYPE4 and not just TYPE4

    0
     

    Author Comment

    by:jewee
    Never mind...I fixed the previous problem but why is it when you have a while loop...while(token)...
    if the token is never found, it does not break out of the loop?

    0
     
    LVL 86

    Expert Comment

    by:jkr
    >>if the token is never found, it does not break out of the loop?

    If the token is not found, it won't even enter the loop, so don't worry about that.
    0
     

    Author Comment

    by:jewee
    it enters the loop looking for that token, and never breaks......
    I changed the test string from "Type :" to "Tydddd :"
    char surStr[1024];
             sprintf(surStr,"digital : hblahb\n"
                        "   Tydddd : CBCL\n"
                        "   Subtype : sdfasdf\n"
                        "   Bit Rate: dfsdfsddf"
                      );


    char tag [] = "Type";
    char result[128];
    char* token;
    token = strtok(surStr, ": \n ");

    while (token)
    {

           if (strstr(token, tag))
           {
       token = strtok(NULL, ": \n");
       printf("TOKEN %s\n ", token);
       break;
       }
    }
    0
     
    LVL 86

    Expert Comment

    by:jkr
    That's not the delimiter I suggested:

    char tag [] = "Type";
    char result[128];
    char* token;
    token = strtok(surStr, "\n ");

    while (token)
    {

          if (strstr(token, tag))
          {
      token = strtok(NULL, ": \n");
      printf("TOKEN %s\n ", token);
      break;
      }
    }
    0
     
    LVL 86

    Accepted Solution

    by:
    Oops, found the reason - you moved the 2nd 'strtok()' call inside the 'if', which is not correct - my suggestion was to

    while ( token) {

       if (strstr(token,tag)) { // found "TYPE :"

           strcpy ( result, token + strlen(tag) + 1);

           break;
       }

       token = strtok(NULL, "\n");
    }

    And, if you also fill the string correctly, it works:

    char surStr[1024];
            sprintf(surStr,"digital : hblahb\n"
                       "   Type : CBCL\n"
                       "   Subtype : sdfasdf\n"
                       "   Bit Rate: dfsdfsddf"
                     );


    char tag [] = "Type :";
    char result[128] = "nothing";
    char* token = NULL;
    token = strtok(surStr, "\n ");


    printf ( "%s", result);

    while ( token) {

       if (strstr(token,tag)) { // found "TYPE :"

           strcpy ( result, token + strlen(tag) + 3);

           break;
       }

       token = strtok(NULL, "\n");
    }

    printf ( "%s", result);

    }
    0
     

    Author Comment

    by:jewee
    great!  it worked!  Thanks!  Quick question, I am using a string to tokenize.  I tried using c_str() within strtok but I received an error re: invalid conversion from const char* to char*...
    0
     
    LVL 17

    Expert Comment

    by:rstaveley
    strtok pokes '\0' characters into the actual string and therefore cannot be used with a const char*
    0
     
    LVL 17

    Assisted Solution

    by:rstaveley
    However, if you want to be wicked you could do something like:

         token = strtok(const_cast<char*>(yourstring.c_str()), "\n ");    // Naughty, naughty!!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now