使用 内置函数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"头文件中 
函数原型:

  1. 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[]="hello@boy@this@is@heima";
char*temp = strtok(buf,"@");
while(temp)
{
printf("%s ",temp);
temp = strtok(NULL,"@");
}
return0;
}

猜你喜欢

转载自www.cnblogs.com/python-xiakaibi/p/9945583.html
今日推荐