loadrunner中文件的操作

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指定的文件中,直到出现字符串结束(’’)为止。
            
         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
",i,data);  //writing datas to the file(filename)
               }
 
        fclose(MyFile1);
        fclose(MyFile2);           
 
        return 0;
}
原文地址:https://www.cnblogs.com/qmfsun/p/4515215.html