Question re: strtok

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?

jeweeAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
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
 
rstaveleyCommented:
Pass strtok the separators "\r\n\t :"
0
 
jkrCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jkrCommented:
>>"\r\n\t :"

That would return a lot of tokens...
0
 
rstaveleyCommented:
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
 
jeweeAuthor Commented:
with the separators
\r\n\t...

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

0
 
jeweeAuthor Commented:
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
 
jkrCommented:
>>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
 
jeweeAuthor Commented:
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
 
jkrCommented:
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
 
jeweeAuthor Commented:
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
 
rstaveleyCommented:
strtok pokes '\0' characters into the actual string and therefore cannot be used with a const char*
0
 
rstaveleyConnect With a Mentor Commented:
However, if you want to be wicked you could do something like:

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

From novice to tech pro — start learning today.