Utility Functions and C Language Reference > Utility Functions: Java (lr.) > Alphabetical Listing > lr_read_file

lr_read_file

Reads a file into a parameter.

C Language

int lr_read_file( const char *filename, const char *outputParam, int continueOnError );

Example C Language String and Parameter Functions

Java Language

int lr.read_file( String <filename>, String <output parameter name>, int <continue on error> );

Example Java Language String and Parameter Functions Java Syntax

Arguments

Name Comments
filename The full pathname or the pathname relative to the script folder.
outputParam The name of the parameter that stores the file contents.
continueOnError LR_EXIT_VUSER (0) or
LR_EXIT_ACTION_AND_CONTINUE (1)

lr_read_file reads a file and saves the file contents in a parameter.

Return Values

This function returns the number of bytes read. On failure, returns a negative number.

Parameterization

Parameterization is not applicable to this function.

Example C Language

These actions show the usages of lr_read_file.


Read a text file:
void read_text_file(){
    int res;
    res = lr_read_file("test1.txt", "test1", 0);
    lr_message("res = %d\n data = %s\n", res, lr_eval_string("{test1}"));
}

Read a binary file:
void read_bin_file(){
    char *raw_data = 0;
    unsigned long raw_data_len = 0;
    int res;

    res = lr_read_file("test2.dat", "test2", 0);
    lr_eval_string_ext("{test2}", 7, &raw_data, &raw_data_len, 0, 0, -1);
    lr_message("res = %d\n raw_data_len = %d\nraw_data = %s", res, raw_data_len, raw_data); 
}

Example Java Language

These actions show the usages of lr.read_file.

public int action() throws Throwable {
    
    
    lr.read_file("test.txt","testparam", lr.EXIT_ACTION_AND_CONTINUE);
    lr.log_message(lr.eval_string("{testparam}"));
    // If file read, outputs file content. If not found or not read, outputs: {testparam}
    
    //not found with error
    lr.read_file("notfound.txt","notfound", lr.EXIT_VUSER);
    /* Log output:
       Error: can not open file
       Abort was called from an action.
       Ending Vuser...
    */
    
    return 0;
}//end of action