Link to home
Start Free TrialLog in
Avatar of jewee
jewee

asked on

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?

Avatar of rstaveley
rstaveley
Flag of United Kingdom of Great Britain and Northern Ireland image

Pass strtok the separators "\r\n\t :"
Avatar of 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");
}
>>"\r\n\t :"

That would return a lot of tokens...
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 :-)
Avatar of jewee
jewee

ASKER

with the separators
\r\n\t...

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

Avatar of jewee

ASKER

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?

>>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.
Avatar of jewee

ASKER

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;
   }
}
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;
  }
}
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jewee

ASKER

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*...
strtok pokes '\0' characters into the actual string and therefore cannot be used with a const char*
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial