LoadRunner/VuGen outputs to external files

The following code works with all versions of LoadRunner

We will use ANSI C fopen(), fprint(), fclose() functions in the LoadRunner to accomplish that. The followings are signatures of those functions:

long fopen( const char *filename, const char *access_mode )

int fprintf( FILE *file_pointer, const char *format_string [, args ] )

Step 1: Open file to add the log

char * filename = "c:\\temp\\logfile.txt";     
char * access_mode = "a+";  
if ((file_stream = fopen(filename, access_mode)) == NULL){ 
    lr_error_message ("Cannot open or create %s", filename);   
    return -1;
}

Step 2: Write to the log file

fprintf(file_stream, "This is the message to output file");

Step 3: Close the log file

//Close the file stream     
fclose(file_stream);

Complete Code

//Name and location of a file to be open or created.     
char * filename = "c:\\temp\\logfile.txt";        
// Open the file with append mode. Other modes : r,r+,w,w+,a,a+,t(text),b(binary)   
char * access_mode = "a+";     
//File pointer     
long file_stream;          
if ((file_stream = fopen(filename, access_mode)) == NULL) {         
lr_error_message ("Cannot open or create %s", filename);         
return -1;     
}     
//Write message to output file     
fprintf(file_stream, "This is the message to output file");          
//Close the file stream     
fclose(file_stream);