Extending Loadrunner Scripts with C – Function Library #1.2

I have now left the Cardiff JMeter role, and returned to Loadrunner work, this time in Dublin.
With every new position I find myself re-invigorated to learn new things, update old code and write some articles for the site.
I have a lot of ideas in my head at the moment, but my shower thoughts this morning were to finally transform the writeToOutputFile function into exactly that, a function.

Previous iterations have hard-coded paths and were generally less than ideal.
And I’m sure this one will be a) better and b) less than perfect.
The problem is that the file is, by necessity and design, opened and closed on every occurrence, which will take a little time.
The alternative would be to gather every call together, build a master string and then output that at one time, perhaps at the end of an iteration, or the end of a test, whatever.
The downside to that would be obvious – unexpected failure would result in nothing being written and potential data-loss, so swings and roundabouts.

I like this one, and in actual fact, it could be called just once at the end of an iteration or test, or multiple times since the file in opened, written and closed inside.

I wrote this as matt.h:

 int writeToOutputFile(char* sTextToWrite, char* sFilename, int iAppend) {

 char sAccessMode[1]; long file_streamer; lr_output_message("sTextToWrite: %s", sTextToWrite); lr_output_message("sFilename: %s", sFilename); lr_output_message("iAppend: %d", iAppend); if (iAppend == 1) { sprintf(sAccessMode, "w"); } if (iAppend == 0) { sprintf(sAccessMode, "a"); } lr_output_message("sAccessMode: %s", sAccessMode); if ((file_streamer = fopen(sFilename, sAccessMode)) == NULL) //open file in append mode { lr_error_message ("Cannot open %s", sFilename); return -1; } fprintf (file_streamer, "%s\n", sTextToWrite); fclose(file_streamer); 

 return 0; } 

So globals.h has a call added:
 #include "matt.h";

And here are a couple examples – mostly used for testing as I wanted to be sure I could send a string and/or a parameter without failure:

 Action() {

 writeToOutputFile("text test text test", "C:\\pde\\text.txt", 0); //0 append, 1 overwrite writeToOutputFile(lr_eval_string("{timestamp}"), "C:\\pde\\text.txt", 0); //0 append, 1 overwrite 

 return 0; }