在LoadRunner中使用檔案的相關函式

TIB發表於2010-01-30

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.

 

相關文章