Thread: Strtok-assigning tokens to structure

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    45

    Strtok-assigning tokens to structure

    Hello everyone...I need to parse some text, I gave a some string just for
    example.
    But I don't know, how to this tokens from strtok assign to different fileds of structure ? (a,b,c,d,e,f,g). For example:

    a=this
    b=is
    c=test
    d= string etc.

    Please, if you have advice to help me.

    [tag]


    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct {
        
        char *a;
        char *b; 
        char *c; 
        int d; 
        char *e; 
        char *f;
        char *g; 
        
    } struct_test;
    
    
    int main ()
     {
        char str[] = "this is a test string::\r\n"
                   "value1: 5\r\n"
                   "value2: 67\r\n\r\n";
                   
                   char *line;
                   char *token;
                   char buf[256];
      
      
      struct_test test;
    
     for (line = strtok (str, "\r\n"); line != NULL; line = strtok (line + strlen (line) + 1, "\r\n")) {
          
          strncpy (buf, line, sizeof (buf));
          printf ("Line: %s\n",buf);
        
      
        for (token = strtok (buf, " "); token != NULL; token = strtok (token + strlen (token) + 1, " ")){
             printf ("\tToken: %s\n",token);
            
            
            }
            
        }
            
        
    return 0;
    
    
    }


    [/tag]

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Hey there

    Have a good look at the example here - strtok, wcstok, _mbstok (CRT)
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    FAQ > Separate a string into tokens? (C) - Cprogramming.com

    You can't use strtok() on two different strings at the same time (as you do in your nested for-loops).

    Bye, Andreas

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I also found this that might help
    Storing a string inside an array?
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    45
    I also found these examples , but there is no explanation how to assigns this tokens to structure....
    AndiPersti, how I can't use it? This code is working...I got tokens, I need just to assigns them to structure :S

    Thx to all

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    45
    Quote Originally Posted by Click_here View Post
    I also found this that might help
    Storing a string inside an array?
    This is similar with my code, but there is no explanation how to assign token to some fileld in structure?

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    505
    strtok() is a bit difficult to use.
    It stores the string it is working on in a global variable. Then it corrupts that string by adding nuls to create the tokens.

    You can assign tokens to a structure with code like

    Code:
    struct_test temp;
    
    temp.d = atoi(token);
    A long-term better solution is to use regular expressions. Unfortunately the C standard runtime library doesn't include them, though most compilers will have them installed. Look for regexpr() inthe documentation.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    45
    I wanted to avoid regular expressions.
    Just to ask, from your example every token will be assigned to temp.d. Can I achieve to assign
    first token to temp.d,
    second token to temp.e,
    third to temp.f etc.

    Is there a way to do this?

    I must to parse HTTP ....I thought that strtok() will solve all my problems, ...now I don't have idea ...

    Thanks very much

    Quote Originally Posted by Malcolm McLean View Post
    strtok() is a bit difficult to use.
    It stores the string it is working on in a global variable. Then it corrupts that string by adding nuls to create the tokens.

    You can assign tokens to a structure with code like

    Code:
    struct_test temp;
    
    temp.d = atoi(token);
    A long-term better solution is to use regular expressions. Unfortunately the C standard runtime library doesn't include them, though most compilers will have them installed. Look for regexpr() inthe documentation.

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by anya View Post
    AndiPersti, how I can't use it? This code is working...I got tokens, I need just to assigns them to structure :S
    You're right. I misread your usage of strtok().

    Assigning a token to a pointer is the same as assigning any other string to a pointer. You have char pointers in your structure so you could just do something like
    Code:
    temp.a = token;
    or you could allocate memory for each pointer and then copy the token string to that memory location using strcpy():
    Code:
    strcpy(temp.a, token);  // assuming you have allocated enough space for "temp.a"
    Quote Originally Posted by anya View Post
    Can I achieve to assign
    first token to temp.d,
    second token to temp.e,
    third to temp.f etc.

    Is there a way to do this?
    With your current structure this would be rather messy. At least you should use a string array for all your tokens.

    Quote Originally Posted by anya View Post
    I must to parse HTTP ....I thought that strtok() will solve all my problems, ...now I don't have idea ...
    But if you want to parse HTTP requests/responses I would use strtok() just for splitting the lines and then use sscanf() to parse the status/request/header lines because they have a well defined format.

    Bye, Andreas

  10. #10
    Registered User
    Join Date
    Jul 2012
    Posts
    45
    Quote Originally Posted by AndiPersti View Post
    You're right. I misread your usage of strtok().

    Assigning a token to a pointer is the same as assigning any other string to a pointer. You have char pointers in your structure so you could just do something like
    Code:
    temp.a = token;
    or you could allocate memory for each pointer and then copy the token string to that memory location using strcpy():
    Code:
    strcpy(temp.a, token);  // assuming you have allocated enough space for "temp.a"



    With your current structure this would be rather messy. At least you should use a string array for all your tokens.


    But if you want to parse HTTP requests/responses I would use strtok() just for splitting the lines and then use sscanf() to parse the status/request/header lines because they have a well defined format.

    Bye, Andreas
    Thank you, I will try to achieve that

    All the best,
    Anya

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assigning value to a char array in a structure
    By Clayg in forum C Programming
    Replies: 11
    Last Post: 11-20-2011, 06:31 PM
  2. Help assigning memory to structure arrays
    By pk68 in forum C++ Programming
    Replies: 10
    Last Post: 11-29-2010, 07:37 AM
  3. Replies: 2
    Last Post: 05-12-2010, 10:10 AM
  4. How to output tokens in reverse order using strtok
    By Ninz in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2003, 09:00 PM
  5. Parsing and Tokens (strtok)
    By readerwhiz in forum C Programming
    Replies: 6
    Last Post: 04-22-2002, 09:57 AM