strtok
Previous Topic  Next Topic 

Synopsis:

       token=strtok (str,tok_delimiter)


Arguments:

       str - The string containing the list of tokens to extract

       tok_delimiters - The set of token delimiters to look for.


Description:    This function searches through the specified str and extracts the tokens one at a time that are separated the specified token_delimiter.       The first call to strtok() should me made with both str and tok_delimeter set to empty strings ("").      This causes all internal counters and data to be reset.     Thereafter the function can be called any number of times to extract the tokens from the string one at a time until there are no tokens left to extract (after which the function will return and empty string ("")).


For example,  lets say that we have a string of values speparated by commas and we want to extract the values from this string one at a time (this is often the case when reading Call Data Records for billing purposes).    E,g,


       CDR="20080320,120603,02082073435,0014153325678,0,360,0.025";


       // Reset the strtok() function

       strtok("","");

       // Extract the fields

       Date=strtok(CDR,",");

       Time=strtok(CDR,",");

       callerID=strtok(CDR,",");

       DestNum=strtok(CDR,",");

       CallStat=strtok(CDR,",");

       Duration=strtok(CDR,",");

       Charge=strtok(CDR,",");


Note that the token_delimiter can be a multiple character delimeter, for example:


           // Notice that the fields are separated by two charatecrs ("::")

       CDR="20080320::120603::02082073435::0014153325678::0::360::0.025";

       

       // Reset the strtok() function

       strtok("","");

       // Extract the fields

       Date=strtok(CDR,"::");

       Time=strtok(CDR,"::");

... etc


Note that for fields separated by tabs, newlines or carriage returns then the escape characters "`t", "`n" and "`r" can be used as the tok_delimiter.


Returns:   Returns the next token from the string or an empty string ("") if there are no more tokens to extract.