loadrunner中文件的操作

我们可以使用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);
实例二  读取字符串
        int MyFile;
        char *ch[10];
        MyFile = fopen("D:\\lr_log\\01.txt","r");
        fscanf(MyFile,"%s", ch);
4、写文件
        fprintf(FILE *stream,const char * format,va_list ap)
       根据参数format字符串来转换并格式化数据,然后将结果输出到参数stream指定的文件中,直到出现字符串结束(’\0’)为止。
           
         int MyFile;
         char ch[] ="Hello World!";
         MyFile = fopen("D:\\lr_log\\01.txt","w");
         fprintf(MyFile,"%s",ch);
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;
}

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lilywu4205/archive/2008/07/17/2664683.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值