strtok

This is a discussion on strtok within the Windows Programming forums, part of the Platform Specific Boards category; Hey, With a string like this: char szSockData[20] = "msg file"; Can someone tell me how i split this into ...

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    strtok

    Hey,

    With a string like this:

    char szSockData[20] = "msg file";

    Can someone tell me how i split this into to seperate strings like this:

    char szSock1[20] = "msg";
    char szSock2[20] = "file";

    I think i need to use strtok but dont know how to use it...

    Thanks
    TNT
    TNT
    You Can Stop Me, But You Cant Stop Us All

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    #include <cstring>
    
    
    int main()
    {
    char str[] = "msg file",
    	 strcopy[50],
    	 *strPtr;
    strcpy(strcopy,str); //make copy to tokenize
    strPtr = strtok(strcopy," ");
    while(strPtr){
    	cout << strPtr << endl;
    	strPtr = strtok(NULL," ");
    	
    }
    return 0;
    }
    The gotch with this function is that it screws any string it touches......the trick is to always tokenize a copy

  3. #3
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Hey Fordy,

    Thanks, im working in windows and i cant quite get it to work, the string i need to split is up is recived through a socket connection, i have put the code like this:

    Code:
    char strcopy[50],
    	 *strPtr;
    
    strcpy(strcopy, recvdata); //recvdata is the str from the socket
    
    strPtr = strtok(strcopy," ");
    
    while(strPtr)
    {
    	strPtr = strtok(NULL," ");
    }

    That compiles fine, but i need it to automatically fill 2 strings with the results from strtok.

    So for instance if recvdata had "msg file" and i have defined:
    char strP1[100];
    char strP2[100];

    i need the While to put "msg" in strP1 and then "file" in strP2.

    I think i have set everything out right, im just getting a bit confused...



    Thanks alot.
    TNT
    TNT
    You Can Stop Me, But You Cant Stop Us All

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Okay...here's something for you to look at.....

    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    #include <cstring>
    
    #define MAX_TOKENS 2  //I only want 2 tokens this time
    
    int main()
    {
    char str[] = "msg file", //Original string recieved from socket
     strcopy[50],//A copy to stop original info being chewed up
     *strPtr, //A pointer which is the return of strtok()
     *strP1[MAX_TOKENS];//Buffers as array of 2 pointers
    int x = 0;
    
    cout << "Text to tokenize = " << str << endl;
    strcpy(strcopy,str); //make copy to tokenize
    
    strPtr = strtok(strcopy," ");//Set Strtok to beginning of strcopy
    
    while(strPtr && x < MAX_TOKENS){//while token is not NULL and less than total tokens
    strP1[x] = new char[100]; //Create space dynamically
    strcpy(strP1[x],strPtr);//Copy token into buffer array
    strPtr = strtok(NULL," ");//Force strtok up the string
    x++;
    }
    for(int y = 0;y<x;y++){
    cout << "Token number " << y+1 << " = " << strP1[y] << endl; //Print results
    delete strP1[y]; //Clean up after myself :)
    }
    return 0;
    }
    This may be a little over the top....but you should get a general idea......

    Instead of 2 seperate buffers for each token as you had it, I have used an array of pointers to strings....

    Why I did this was to make the code fore flexable.....

    So if the string to tokenise was say 3 words, then all I would have to do is change the Macro at the top of the code to reflect this. For instance;

    Code:
    #define MAX_TOKENS 3
    Now it would handle a maximum of 3 tokens, and would not choke if there were actually less.........

    Have a play around with it and come back if you have more problems

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    22

    strtok

    Code:
    /* consider the prototype */
    char *strtok(char *one, const char *two);
    This function is used to separate string one in our case into tokens separated by characters from the string two. The first call to strtok should include string one, and, to continue from the end of the previous token use NULL in the place of one in the subsequent calls. e.g

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
      char szSockData[20] = "msg file"; 
      char *temp; /* temporary pointer to store the tokens before
                            transferring them*/
      char szSock1[20]; 
      char szSock2[20]; 
    
    /*this is quite simple! :) */ 
      temp = strtok(szSockData, " "); 
      memcpy(szSock1, temp, 20);
      temp = strtok(NULL, " "); 
      memcpy(szSock2, temp, 20);
      printf("\nszSock1: %s\nszSock2: %s\n", szSock1, szSock2);
    }
    This results in:
    Code:
    szSock1: msg
    szSock2: file
    Please note that you can use more than one character in string two (refer to the prototype above) e.g you could try this out
    Code:
    #include <stdio.h>
    #include <string.h>
    #define SEPARATING_CHARS "; ,."
    /*tokens will be separated by any of the above characters */
    
    int main()
    {
      char szSentence[]="Hey, how're you doing?, he asked. Fine";
      char *temp;
    
      temp = strtok(szSentence, SEPARATING_CHARS);
      printf("\nThe words of this sentence are:\n%s\n", temp);
      while((temp = strtok(NULL, SEPARATING_CHARS)) != NULL) {
         printf("%s\n", temp);
      }
    }
    The output of this small program is:
    Code:
    The words of this sentence are:
    Hey
    how're
    you
    doing?
    he
    asked
    Fine
    Hope this has been of help, let me know...

    Cheers.
    -------------------------
    Gerald.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 09:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21