使用 內建函式strtok()函式實現 loadrunner 字串替換
Action()
{
/* loadrunner 字串替換 */
char separators[] = "/";
char * token;
char * file_path;
char * file_path_record;
int len_file_path = 0;
extern char * strtok(char * string, const char * delimiters ); // Explicit declaration
int i=0;
file_path= (char *)calloc(200, sizeof(char));
lr_save_string("http://192.168.70.65:8888/group1/M00/00/A0/wKhGQVvo51SAKaaRAAPCOT1ZRgY858.png","return_file_path");
strcat(file_path,lr_eval_string("{return_file_path}"));
token = (char *)strtok(file_path, separators); // Get the first token
if (!token) {
lr_output_message ("No tokens found in string!");
return( -1 );
}
// lr_output_message ("*****%s", token );
len_file_path=200+strlen(token);
file_path= (char *)calloc(len_file_path, sizeof(char));
file_path_record= (char *)calloc(len_file_path, sizeof(char));
while (token != NULL ) { // While valid tokens are returned
strcat(file_path, token);
strcat(file_path_record, token);
if (i==0)
{
lr_output_message("\\\\\\\\\\\\/");
lr_output_message("\\\\/");
lr_output_message(file_path);
strcat(file_path,"\\\\\\\\\\\\/");
strcat(file_path,"\\\\\\\\\\\\/");
strcat(file_path_record,"\\\\/");
strcat(file_path_record,"\\\\/");
lr_output_message(file_path);
}
else if (i==6)
{
strcat(file_path,"");
strcat(file_path_record,"");
}
else
{
strcat(file_path,"\\\\\\\\\\\\/");
strcat(file_path_record,"\\\\/");
}
lr_output_message ("第%d個字串:%s", i,token );
i+=1;
token = (char *)strtok(NULL, separators); // Get the next token
}
lr_save_string(lr_eval_string(file_path), "param_file_path");
lr_save_string(lr_eval_string(file_path_record), "param_file_path_record");
lr_output_message(lr_eval_string("{param_file_path}"));
lr_output_message(lr_eval_string("{param_file_path_record}"));
return 0;
}
char *strtok( char *strToken, const char *strDelimit );
Strtok()函式詳解:
該函式包含在"string.h"標頭檔案中
函式原型:
-
char* strtok (char* str,constchar* delimiters );
函式功能:
切割字串,將str切分成一個個子串
函式引數:
str:在第一次被呼叫的時間str是傳入需要被切割字串的首地址;在後面呼叫的時間傳入NULL。
delimiters:表示切割字串(字串中每個字元都會 當作分割符)。
函式返回值:
當s中的字元查詢到末尾時,返回NULL;
如果查不到delimiter所標示的字元,則返回當前strtok的字串的指標。
#include<stdio.h>
#include<string.h>
int main(void)
{
char buf[]="[email protected]@[email protected]@heima";
char*temp = strtok(buf,"@");
while(temp)
{
printf("%s ",temp);
temp = strtok(NULL,"@");
}
return0;
}