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?
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?
Pass strtok the separators "\r\n\t :"
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");
}
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...
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 :-)
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 :-)
ASKER
with the separators
\r\n\t...
it will only return the line:
TYPE : TYPE4 and not just TYPE4
\r\n\t...
it will only return the line:
TYPE : TYPE4 and not just TYPE4
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 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.
If the token is not found, it won't even enter the loop, so don't worry about that.
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;
}
}
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;
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.