LoadRunner Strtok function

Question ID: 104856

I have a string in LR that has different characters that has separate the fields in the data, how do I parse the string. The string contains "/n" carriage return character and other separators. I need to separate each field.

I need some help with the syntax for this

Marked as spam
Posted by (Questions: 218, Answers: 15)
Asked on February 15, 2013 9:53 pm
Answers (1)
Private answer

I have attached a code fragment that illustrates how to separate a string from within a string that is separated by more than one delimiter. You can drop the code below directly into LoadRunner and the code will execute. I recommend that you step through the code while it is executing using the F10 Key to see how this actually functions. The code is heavily commented to provide illumination to your understanding. This sample code will work in all versions of Vugen without issue. Just create a new C Vuser script and replace the action section in total.

char str[] = ''a;b;c;d;e/nf;g;h;i;j/n1;2;3;4;5/n'';
char *end_str;

/********* The separator is what strtok uses to find breaks between the tokens. *********/
/********* You can use multiple separators. All must be a single character. *********/
/********* Here we use a '';'' and a ''/n'', this gets all tokens in this string. *********/

/********* Note the double slash in front of the ''/n'' separator. This has to be treated as a literal *********/
char separator[] = '';//n'';

/********* The normal strtok function in C returns a pointer to character string. LR returns a long, which is actually a generic pointer to the string *********/
long token=NULL;

/********* END OF DECLARATIONS *********/

/********* Display the contents of str *********/
lr_message(''str = %s'', str);

/********* Find the first token *********/
token=strtok(str, separator);
/********* token is a long, display it cast as a ptr to a char *********/
lr_message(''token = %s'', (char *)token);

/********* If the call to strtok returns 0, we have an error *********/
if( !token )
lr_message( ''No tokens found in source string!'' );
return( -1 );

/********* Now loop through the string, separating out each token based upon all of the tokens *********/
while (token != NULL)
token = strtok( NULL,separator );
lr_message(''token = %s'', token);

return 0;

Marked as spam
Posted by (Questions: 4, Answers: 41)
Answered on February 15, 2013 10:02 pm