Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Question re: strtok

Posted on 2004-11-01
13
Medium Priority
?
310 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
Comment
Question by:jewee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
13 Comments
 
LVL 17

Expert Comment

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

Expert Comment

by:jkr
ID: 12465175
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
ID: 12465187
>>"\r\n\t :"

That would return a lot of tokens...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 17

Expert Comment

by:rstaveley
ID: 12465231
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
ID: 12465505
with the separators
\r\n\t...

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

0
 

Author Comment

by:jewee
ID: 12465567
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
ID: 12465656
>>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
ID: 12465782
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
ID: 12465856
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:
jkr earned 1200 total points
ID: 12466454
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
ID: 12466946
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
ID: 12467056
strtok pokes '\0' characters into the actual string and therefore cannot be used with a const char*
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 800 total points
ID: 12467078
However, if you want to be wicked you could do something like:

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

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

636 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