美轮美奂的老臭虫
[转载]Loadrunner中文件的操作
上一篇 /
下一篇 2008-08-28 21:31:20
/ 个人分类:性能测试
我们可以使用fopen()、fscanf()、fprintf()、fclose()函数进行文件操作,但是因为
LoadRunner不支持FILE数据类型,所以我们需要做以下修改:
1、文件的声明
先定义一个int类型的文件指针:
int MyFile;
2、文件的打开
fopen(FILE * stream ,const char *format,....):返回一个FILE数据类型的指针。
因为LoadRunner不支持FILE数据类型,所以我们要将返回值转化成int型。其中,第一个参数指定文件路径;第二个参数指定创建文件的模式。
int MyFile;
MyFile=(int)fopen("D:\\lr_log\\01.txt","r");
3、读文件
fscanf(FILE * stream ,const char *format,....)
自参数stream的文件流中读取字符串,再根据参数format字符串来转换并格式化数据:
实例一 读取数值型数据
int MyFile;
int number;
MyFile = fopen("D:\\lr_log\\01.txt","r");
fscanf(MyFile,"%d", &number);
实例二 读取字符串
Action()
{
int MyFile;
char *ch[10];
MyFile = (int)fopen("D:\\temp\\data2.txt","r+");
fscanf(MyFile,"%s", ch);
lr_output_message("%s",ch);
return 0;
}
4、写文件
fprintf(FILE *stream,const char * format,va_list ap)
根据参数format字符串来转换并格式化数据,然后将结果输出到参数stream指定的文件中,直到出现字符串结束(’\0’)为止。
Action()
{
int myfile;
char ch[] ="Hello World! My English name is Cheers_Lee";
myfile=(int)fopen("D:\\temp\\data2.txt","w");
fprintf(myfile,"%s",ch);
return 0;
}
5、关闭文件
fclose(FILE * stream)
下面贴一个简单的实例,从一个文件读取数据写入另一个文件中
Action()
{
int MyFile1,MyFile2;
int i;
char data[80]; //defining a parameter,using for storing datas
// Assigning the file path to a string
char *addr="D:\\lr_log\\01.txt";
char *addr1="D:\\lr_log\\user.txt";
//if fail to open the file,print error message
if((MyFile1 = fopen(addr,"r"))==NULL||(MyFile2=fopen(addr1,"w+"))==NULL)
{
lr_error_message("Can't open this file",addr);
return -1;
}
if((MyFile2= fopen(addr1,"w+"))==NULL)
{
lr_error_message("Can't open this file",addr1);
return -1;
}
for(i=1;i<=6;i++)
{
fscanf(MyFile1,"%s",data); //reading the datas from MyFile to the string(data)
lr_output_message("Line%d: %s",i,data);
fprintf(MyFile2,"第%d个用户:%s\n",i,data); //writing datas to the file(filename)
}
fclose(MyFile1);
fclose(MyFile2);
return 0;
}
收藏
举报
TAG:
测试工作学习