Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 10th, 2009, 8:21 AM   #1
MFranklin1981
C Student
 
MFranklin1981's Avatar
 
Join Date: Feb 2007
Location: Seattle
Posts: 69
Rep Power: 8 MFranklin1981 is on a distinguished road
How to use strtok

I cant figure out how to assign parse_struct->name and parse_struct-value the value stored in token.

Would someone mind pointing me in the right direction?

Test Data:

c Syntax (Toggle Plain Text)
  1. int main( void )
  2. {
  3. int inx 0;
  4. struct parse_s parse;
  5. char * test[] = { "123.456 some-number-name",
  6. " 314.159 pi*100",
  7. "314159 pi*100000",
  8. "0.314159 pi/10",
  9. "123.45 123.45 ",
  10. "314.159 pi*100 ",
  11. "-123.456 a-neg-number ",
  12. "0.0 zero-number",
  13. "0 another-zero",
  14. /** these should fail */
  15. "123.456 some-really-long-name-string",
  16. "3.1.4159 pi",
  17. "499.999 some-number 299.999 extra ",
  18. "",
  19. "no-num",
  20. "invalid-num xxx",
  21. "name-in-wrong-position 123.45",
  22. "123.45 "
  23. };
  24. ...
  25. parse_strtok( test[inx], &parse );
  26. ...
  27.  
  28. }
  29.  
  30. static int parse_strtok( const char *in_str, struct parse_s *parse_struct )
  31. {
  32. assert ( in_str != NULL );
  33.  
  34. char *token = NULL;
  35. char buffer[BUFFSIZE];
  36.  
  37. strncpy(buffer, in_str, BUFFSIZE - 1);
  38.  
  39. buffer[BUFFSIZE - 1] = '\000';
  40. token = strtok(buffer, " \t");
  41.  
  42. while (token != NULL)
  43. {
  44. /* how do I get the correct value of token into parse_struct->value then into parse_struct->name? */
  45. parse_struct->value = *token; /* assigns 49 to value */
  46. strcpy(parse_struct->name, token); /* assigns 123.456 to name. eh? */
  47. token = strtok(NULL, " \t");
  48. }
  49. return FALSE;
  50. }
__________________
I'm running in the Seattle Rock 'n' Roll Marathon. Help me fight cancer by donating or forwarding this link on...
http://pages.teamintraining.org/wa/r...l09/mfrankltrt
MFranklin1981 is offline   Reply With Quote
Old Apr 10th, 2009, 9:46 AM   #2
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
Re: How to use strtok

line 39: delete it because the last byte of a string must be '\0', and strcpy() does that.

lines 45 and 46: you can't just assign strings like that -- you have to allocate memory for the string then use strcpy() to copy the string.

line 45: *token -- that only points to one character, not an entire string.
Ancient Dragon is offline   Reply With Quote
Old Apr 10th, 2009, 11:01 AM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Google / Kitchener
Posts: 4,154
Rep Power: 15 Sane has a spectacular aura aboutSane has a spectacular aura about
Send a message via MSN to Sane
Re: How to use strtok

C in a Nutshell (Chapter 17, 440-41):

strtok
Divides a string into tokens

#include <string.h>
char *strtok( char * restrict s1, const char * restrict s2 );

The strtok() function isolates tokens in the string addressed by s1 that are delimited by any of the characters contained in the string addressed by s2. The tokens are identified one at a time by successive calls to strtok(). On calls after the first, the s1 argument is a null pointer.

On the first call, strtok() searches in s1 for the first character that does not match any character in s2, behavior that is similar to the strspn() function. The first such character found is considered to be the beginning of a token. Then strtok() searches further for the first character that does match any of the characters in s2 -- or the null character that terminates the string, whichever comes first -- similarly to the strcspn() function. This is considered to be the delimiter that ends the token. strtok() then replaces this ending delimiter with '\0', and returns a pointer to the beginning of the token (or a null pointer if no token was found), while saving an internal, static pointer to the next character after the ending delimiter for use in subsequent strtok() calls.

On each subsequent call with a null pointer as the s1 argument, strtok() behaves similarly, but starts the search at the character that follows the previous delimiter. You can specify a different set of delimiters in the s2 argument on each call. The locations that strtok() reads from using s2 and writes to using s1 on any given call must not overlap.

Example
char *mnemonic, *arg1, *arg2, *comment;
char line[] = "    mul eax,[ebp+4]    ; Multiply by y\n";

mnemonic = strtok( line, " \t" ); // First word, between spaces or tabs.
arg1 = strtok( NULL, "," );       // From there to the comma is arg1.
                                  // (Trim off any spaces later.)
arg2 = strtok( NULL, ";\n" );     // From there to a semicolon or line end.
comment = strtok( NULL, "\n\r\v\f" ); // From there to end of line or page.

printf( "Command:      %s\n"
        "1st argument: %s\n"
        "2nd argument: %s\n"
        "Comment:      %s\n\n",
        mnemonic, arg2, arg2, comment );

Output
Command:      mul
1st argument: eax
2nd argument: [ebp+4]
Comment:       Multiply by y
__________________
PFO's Folding@Home Team | Sane's Monthly Algorithms Challenges
Rules | How to Post a Question | How to Post Code

Becoming a good programmer requires foresight of your code's execution.
Becoming an excellent programmer requires foresight of your code's modification.
Sane is offline   Reply With Quote
Old Apr 13th, 2009, 10:45 PM   #4
MFranklin1981
C Student
 
MFranklin1981's Avatar
 
Join Date: Feb 2007
Location: Seattle
Posts: 69
Rep Power: 8 MFranklin1981 is on a distinguished road
Re: How to use strtok

Once again, many thanks to Sane and Ancient Dragon! I'm still having a few problems with validating my data. I can successfully parse valid data. But I can only catch two failure conditions (string too big and string in wrong position). I'm especially having troubles figuring out how to construct this program so it returns FALSE when the token returns a value it cannot convert into either a floating point value or a valid string.
Here is my function (test data).
c Syntax (Toggle Plain Text)
  1. /***************************************************************************
  2.  * Function: parse_strtok
  3.  * Description: parse string strtok and other stdlib functions
  4.  * Input:
  5.  * in_str - string to parse
  6.  * Output:
  7.  * parse_struct - pointer to structure to add data to
  8.  * Return:
  9.  * TRUE - sucessfully parsed and updated parse_struct
  10.  * FALSE - some error encountered
  11.  * Special Logic:
  12.  * When FALSE returned, parse_struct is NOT modified
  13.  * Input and Output pointers must be non-NULL.
  14.  ***************************************************************************/
  15. static int parse_strtok( const char *in_str, struct parse_s *parse_struct )
  16. {
  17. char buffer[BUFFSIZE];
  18. double val;
  19. char *endptr = NULL;
  20. char *token = NULL;
  21. int valid_input = TRUE;
  22.  
  23. errno = 0;
  24.  
  25. strncpy(buffer, in_str, BUFFSIZE - 1);
  26. buffer[BUFFSIZE - 1] = '\000';
  27.  
  28. val = strtod(in_str, &endptr);
  29.  
  30. if ( errno != 0 || *endptr != '\000' )
  31. {
  32. token = strtok(buffer, " ");
  33.  
  34. while ( token != NULL )
  35. {
  36. if((strlen(token) > NAME_LEN))
  37. {
  38. valid_input = FALSE;
  39. }
  40. strcpy(parse_struct->name, token);
  41. token = strtok(NULL, " ");
  42. }
  43. }
  44.  
  45. parse_struct->value = val;
  46.  
  47. return valid_input;
  48. }
Output...
********************
Testing parse_strtok
********************
Success parsed >>123.456 some-number-name<< to >>some-number-name<< and 123.456000
Success parsed >> 314.159 pi*100<< to >>pi*100<< and 314.159000
Success parsed >>314159 pi*100000<< to >>pi*100000<< and 314159.000000
Success parsed >>0.314159 pi/10<< to >>pi/10<< and 0.314159
Success parsed >>123.45 123.45 << to >>123.45<< and 123.450000
Success parsed >>314.159 pi*100 << to >>pi*100<< and 314.159000
Success parsed >>-123.456 a-neg-number << to >>a-neg-number<< and -123.456000
Success parsed >>0.0 zero-number<< to >>zero-number<< and 0.000000
Success parsed >>0 another-zero<< to >>another-zero<< and 0.000000
Failed to parse >>123.456 some-really-long-name-string<<
Success parsed >>3.1.4159 pi<< to >>pi<< and 3.100000
Success parsed >>499.999 some-number 299.999 extra << to >>extra<< and 499.999000
Success parsed >><< to >><< and 0.000000
Success parsed >>no-num<< to >>no-num<< and 0.000000
Success parsed >>invalid-num xxx<< to >>xxx<< and 0.000000
Failed to parse >>name-in-wrong-position 123.45<<
Success parsed >>123.45 << to >>123.45<< and 123.450000
__________________
I'm running in the Seattle Rock 'n' Roll Marathon. Help me fight cancer by donating or forwarding this link on...
http://pages.teamintraining.org/wa/r...l09/mfrankltrt

Last edited by MFranklin1981; Apr 13th, 2009 at 11:15 PM. Reason: Linked Test Data
MFranklin1981 is offline   Reply With Quote
Old Apr 13th, 2009, 11:04 PM   #5
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
Re: How to use strtok

put a break statement after line 38 because you don't want to continue the loop or execute line 40 when valid_input == FALSE. If the length of the string is too long then line 40 will probably corrupt memory.
Ancient Dragon is offline   Reply With Quote
Old Apr 18th, 2009, 7:09 AM   #6
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
Re: How to use strtok

Quote:
Originally Posted by Kyung Ji View Post
The C++ strtok function tokenizes a string. It splits a source string into segments called tokens that are separated by any of a specified set of characters called delimiters. Strtok takes the source string as an argument on its first call to scan for delimiters. It then uses the position after the end of the last token to scan for the next token in subsequent calls.
Yes, I believe that has been said several times before, had you bothered to read the thread.
Ancient Dragon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
converting string to float beginnerCCC C 22 Oct 2nd, 2006 11:59 PM
convert this based on its length pastalover C 16 Jun 14th, 2006 10:17 PM
Pointers! Why? LOI Kratong C++ 33 Dec 17th, 2005 11:33 PM
Function that returns occurances of substring in string. conbrio C 7 Jul 1st, 2005 6:51 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:00 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC