loadrunner中目录和文件操作函数

 

Writes a string to the end of a file.

Arguments:

 - file_name: Include the full path in the file name, and escape any slashes. E.g. "C:\\TEMP\\output.txt". Note that file does not have to exist beforehand, but directory does.

 - string: If attempting to write a single line, include a newline character at the end of the string.

Returns 0 on success. On failure, function will raise lr_error_message and return -1.

*/

(1)把字符串写到文件末尾

int lr_append_to_file(char* file_name, char* string) {

  int fp; // file pointer

  int rc; // return code

  int length = strlen(string);

 

  // Check that file_name is not NULL.

  if (file_name == NULL) {

    lr_error_message("Error. File name is NULL");

       return -1;

  }

 

  fp = fopen(file_name, "a"); // open file in "append" mode.

  if (fp == NULL) {

    lr_error_message("Error opening file: %s", file_name);

    return -1;

  }

 

  rc = fprintf(fp, "%s", string);

  if (rc != length) {

     lr_error_message("Error writing to file: %s", file_name);

     return -1;

  }

 

  rc = fclose(fp);

  if (rc != 0) {

    lr_error_message("Error closing file: %s", file_name);

    return -1;

  }

 

  return 0;

}

 

//

 

// Checks if a file already exists on the filesystem.

// Arguments:

//  - file_name: Include the full path in the file name.

// Returns TRUE (1) if file exists and user has read access to the file, otherwise function returns FALSE (0).

 

(2)检查指定文件是否存在

int lr_file_exists(char* file_name) {

  int fp; // file pointer

 

  fp = fopen(file_name, "r+"); // open file in read mode. File must already exist.

  if (fp == NULL) {

    return FALSE;

  } else {

    fclose(fp);

    return TRUE;

  }

}

 

//

// Saves a file to the hard disk.

// Arguments:

// - file_name: Include the full path in the file name. Note that file must not exist before function is called.

// - file_content: The data to save to the file. Can be binary or string data.

// - file_size: The size/length of the data to save to the file. If it is string data, you can find this using strlen(). If you are saving binary data from a web page, use web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE).

// Returns 0 on success. On failure, function will raise lr_error_message and return -1.

(3)保存文件到磁盘

int lr_save_file(char* file_name, void* file_content, unsigned int file_size) {

  int rc; // function return code

  int fp; // file pointer

 

  // Check input values

  if (file_name == NULL) {

    lr_error_message("File name is NULL");

    return -1;

  } else if (file_content == NULL) {

    lr_error_message("File content is NULL");

    return -1;

  } else if (file_size < 1) {

    lr_error_message("Invalid file size: %d", file_size);

    return -1;

  }

 

  // Does the file already exist?

  if (lr_file_exists(file_name) == TRUE) {

    lr_error_message("File %s already exists", file_name);

    return -1;

  }

 

  fp = fopen(file_name, "wb"); // open file in "write, binary" mode.

  if (fp == NULL) {

    lr_error_message("Error opening file: %s", file_name);

    return -1;

  }

 

  rc = fwrite(file_content, file_size, 1, fp);

  if (rc != 1) {

    lr_error_message("Error writing to file. Items written: %d", rc);

    return -1;

  }

 

  rc = fclose(fp);

  if (rc != 0) {

    lr_error_message("Error closing file: %s", file_name);

    return -1;

  }

 

  return 0;

}

 

 

 

 

使用的例子:

Action()

{

       int is_exists=0;

       char *str = "string to write!";

       char *file= "D:\\test.txt";

 

       is_exists = lr_file_exists( file );

    //lr_save_int(is_exists,"File_Exist");

       //lr_output_message(lr_eval_string("{File_Exist}"));

 

       if(is_exists)

       {

              lr_append_to_file(file,str);

       }

       else

       {

              lr_save_file(file,str,strlen(str));

       }

 

       return 0;

}

 

 

 

 

LR的帮助文档中,也有关于文件、目录操作函数的详细介绍:

Function Name

Description

chdir

更改当前目录到指定的路径

chdrive

切换到另外一个驱动器

getcwd

返回当前工作目录的名称

getdrive

返回当前驱动器的名称

mkdir

创建目录

remove

删除指定文件

rmdir

删除指定文件夹

 

 

Function Name

Description

fclose

关闭文件

feof

检查是否到文件流末尾

ferror

检查是否出现io错误

fgetc

从文件流中读取一个字符

fgets

从文件中读取一个字符串

fopen

打开文件

fprintf

往一个文件中写入格式化的文本

fputc

往文件流写入一个字符

fread

从文件流读入未格式化的数据

fscanf

从文件流读入格式化的数据

fseek

把当前位置指向文件中的新的位置

fwrite

把未格式化的数据写入文件流

rewind

指向文件开头

sprintf

往字符串写入格式化的数据

sscanf

往字符串读入格式化的数据.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值