在LoadRunner中使用文件的相关函数

本文介绍在LoadRunner测试中如何封装并使用文件操作函数,包括文件写入、检查文件是否存在及保存文件等内容。提供了示例代码及LR内置文件操作函数列表。

LoadRunner测试过程中,有时候需要使用文件,可以封装出几个常用的文件操作函数。

参考:

http://www.jds.net.au/tech-tips/vugen-code-snippets/

/*

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.

*/

int jds_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).

int jds_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.

int jds_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 (jds_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 = jds_file_exists( file );

//lr_save_int(is_exists,"File_Exist");

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

if(is_exists)

{

jds_append_to_file(file,str);

}

else

{

jds_save_file(file,str,strlen(str));

}

return 0;

}

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

Function Name

Description

chdir

Changes the current directory to the given path.

chdrive

Switches to another drive.

getcwd

Returns the name of the current working directory.

getdrive

Returns the name of the current drive.

mkdir

Creates a directory using the given path name.

remove

Deletes the specified file.

rmdir

Deletes the specified directory.

Function Name

Description

fclose

Closes a file.

feof

Checks if the end of file has occurred on a stream.

ferror

Checks if any error has occurred during file I/0.

fgetc

Gets a character from a stream.

fgets

Reads a string from a file.

fopen

Opens a file for buffered I/0.

fprintf

Writes formatted output to a file.

fputc

Writes a character to a stream.

fread

Reads unformatted data from a stream into a buffer.

fscanf

Reads formatted input from a stream.

fseek

Sets the current position in a file to a new location.

fwrite

Write unformatted data from a buffer to a stream.

rewind

Rewinds a file.

sprintf

Writes formatted output to a string.

sscanf

Reads formatted input from a string.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值