C语言在LoadRunner中应用-学习sscanf函数

上一篇 / 下一篇  2007-07-23 14:06:44

今天看到关于“C语言在LoadRunner中应用”的文章,帖子的链接地址如下所示:
http://tech.groups.yahoo.com/group/LoadRunnerCN/message/29
感觉不错。在学校时学习过c语言,不过那时主要为了考试,没有过细的追问些什么,今天看到关于c语言在loadrunner中的应用文章,是从c基础开始讲的,于是借着这个机会,温习一下c的知识并进一步学习其在loadrunner的应用。
正式的学习是从一个例子开始的,涉及到lr_output_message和sscanf()函数。主要是想了解一下scanf()函数在loadrunner中的使用情况。在lr自带的帮助中查找到sscanf()的语法及其自带的例子,例子如下:

The following example uses sscanf to read the string sentence and retrieve the first 3 elements from it. It then sends these elements, s1, num_of_years, and s2, in an output message.

Action() {

char sentence[] = "After 7 years' siege yet Troy walls stand";
char s1[32], s2[32];
int num_of_years;

sscanf(sentence,"%s %d %s", s1, &num_of_years, s2);
lr_output_message("Number of years=%d s2=\"%s\"", num_of_years, s2);

return 0;
}

Output:
Action.c(7): Number of years=7 s2="years'"

从输出结果中观察到,sscanf()函数将数组sentence中的值以空格符为分隔赋s1和s2,而时长num_of_years使用了引用,感觉很巧妙,自己以后还是要多学习lr自带的例子以期让自己的代码更精炼。
借着这个引用的提示,我在学习c的流程控制章节时,将文章中的例子稍微做了一下修改,代码如下:
char sentence[] = "MyCPSScore is 86 .";
char s1[32], s2[32];
int num = 80;
int* MyCPSScore = #

sscanf(sentence,"%s %s %d", s1, s2, MyCPSScore);
lr_output_message("The score is=%d", *MyCPSScore);

if(*MyCPSScore< 75){
lr_output_message(">>>>>>>>>>> Bad luck, try again!!");
}
else if((*MyCPSScore>= 75) && (*MyCPSScore<= 85)){
lr_output_message(">>>>>>>>>>> Good Job!");
}
else if((*MyCPSScore> 85) && (*MyCPSScore<= 100)){
lr_output_message(">>>>>>>>> Excellent, Keep up the goodwork!");
}
else{
lr_output_message(">>>>>>>>>>> Invalid score!");
}

输出:vuser_init.c(9): The score is=86
     vuser_init.c(18): >>>>>>>>> Excellent, Keep up the goodwork!
在此处虽然是正确的,但是有一点我不知道这样的编程风格是否合理,希望大家多多指教。


TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-29  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 19382
  • 日志数: 22
  • 书签数: 21
  • 建立时间: 2007-05-17
  • 更新时间: 2009-03-01

RSS订阅

Open Toolbar