Testing is Rocket Science Not Brain Surgery

Almost all of the World's Greatest Accomplishments Were The Result of Great Planning!

Testing is Rocket Science Not Brain Surgery header image 2

Code Snippet(HP LoadRunner) : FILE I/O and creating dummy XMLs

September 27th, 2012 · 7 Comments · Uncategorized

This is a code dump of a XML file generator I threw together to create multiple child nodes under what was essentially a header in the XML file, along with generating multiple files iteratively. The actual number of iterations was controlled by the number of iterations specified in the run-time settings, the number of child nodes to create is a parameter. I’ll only include a slice of code in this post, the entire file should be available for download with the post. This was done to support a custom web services testing effort involving electronic medical record(EMR) transfers.Create Sync Data File

Create_Test_File(char *header_filename, char *output_filename, int merge_count, int max_merge_count)
{
int count, output,patient_record_length,result_record_length,tail_record_length;

int result_lines,num_of_result_lines;

int total = 0;

char buffer[20000];

char * buffer_manual;

long h_file_stream;
long o_file_stream;

num_of_result_lines = atoi(lr_eval_string(“{Max_Num_Result_Lines}”));

//char * filename = “c:\\readme.txt”;

// Open the file with read access
/*if (merge_count == max_merge_count) {

if ((h_file_stream = fopen(header_filename, “rb”)) == NULL) {

lr_error_message (“Cannot open %s”, header_filename);

return -1;

}

// Read until end of file

while (!feof(h_file_stream)) {

// Read 1000 bytes while maintaining a running count

count = fread(buffer, sizeof(char), sizeof(buffer), h_file_stream);

lr_output_message (“%3d read”, count);

// Check for file I/O errors

if (ferror(h_file_stream)) {

lr_output_message (“Error reading file %s”, header_filename);

break;

}

total += count; // Add up actual bytes read

}

// Display final total

lr_output_message (“Total number of bytes read = %d”, total );*/

if ((o_file_stream = fopen(output_filename, “ab”)) == NULL) {

lr_error_message (“Cannot open %s”, output_filename);

return -1;

}

//Write Patient Header
/*output = fwrite(buffer, sizeof(char), total, o_file_stream);
//fwrite(&sd, sizeof(struct student_data), 1, file);

total = 0;

if (output > 0) {

lr_output_message (“Successfully wrote a header of size %d bytes”, output);
}*/

//buffer_manual = lr_eval_string(“\n\r

lr_advance_param(“Root0_Part1”);

buffer_manual = lr_eval_string(“\n\r
“\n\rFA03C9202EF348F484C42923B21B521C
“\n\r
“\n\rPERFTEST
“\n\rPatient-{PatientName_Serial}
“\n\r1980-06-26T00:00:00
“\n\rDr.
“\n\rMyMiddleName
//”\n\r13179999999
//”\n\r1980-06-26T00:00:00
“\n\retc
“\n\r
“\n\rcdf.gen.female
“\n\r

“\n\r
“\n\r

“\n\r
“\n\r
“\n\rN
“\n\r

//”\n\r
//”\n\rY
//”\n\r

“\n\r

“\n\r

“\n\r“);

// Close the file stream

/*if (fclose(h_file_stream))
{

lr_error_message (“Error closing file %s”, header_filename);

}*/

//} //terminate loop for header and result qualifier info
//Write CDF Patient Data

patient_record_length = strlen(buffer_manual);

output = fwrite(buffer_manual, sizeof(char), patient_record_length, o_file_stream);
//fwrite(&sd, sizeof(struct student_data), 1, file);

total += output; // Add up actual bytes wrote

if (output > 0) {

lr_output_message (“Successfully wrote a patient record of size %d bytes”, total);
}

total = 0;

for (result_lines = num_of_result_lines; result_lines >= 0; result_lines–)

{

lr_advance_param(“Root2_Part1”);
lr_advance_param(“Root2_Part2”);

buffer_manual = lr_eval_string(“\n\r
“\n\r
“\n\rcdf.res.measurement
“\n\r

“\n\r
“\n\r2011-02-13T04:10:49
“\n\r

“\n\r0
“\n\rN
“\n\rN
“\n\rN
“\n\r
“\n\rcdf.dsrc.device.insulinpump
“\n\r

“\n\r79/2030670
“\n\r
“\n\rcdf.dat.bg
“\n\r

“\n\r
“\n\r
“\n\r
“\n\rcdf.res.monitoring.upr.bolus
“\n\r

“\n\r
“\n\r2011-02-13T04:10:49
“\n\r

“\n\r1
“\n\rY
“\n\rN
“\n\rN
“\n\r
“\n\rcdf.dsrc.device.insulinpump
“\n\r

“\n\r79/2030670
“\n\r
“\n\rcdf.dat.insulin.dose
“\n\r

“\n\r

“\n\r

“\n\r
“);

//Write Results
result_record_length = strlen(buffer_manual);

output = fwrite(buffer_manual, sizeof(char), result_record_length, o_file_stream);
//fwrite(&sd, sizeof(struct student_data), 1, file);

total += output; // Add up actual bytes wrote

if (output > 0) {

lr_output_message (“Successfully wrote a result record of size %d bytes”, total);
}

total = 0;

lr_output_message (“Successfully wrote a result record number %d “, result_lines);

}

buffer_manual = lr_eval_string(“\n\r”
“\n\r“);

//Close Results Node
result_record_length = strlen(buffer_manual);

output = fwrite(buffer_manual, sizeof(char), result_record_length, o_file_stream);
//fwrite(&sd, sizeof(struct student_data), 1, file);

total += output; // Add up actual bytes wrote

if (output > 0) {

lr_output_message (“Successfully closed a result record of size %d bytes”, total);
}

total = 0;

// Close the file stream

if (merge_count == 0) {

buffer_manual = lr_eval_string(“\n\r”
“\n\r”
“\n\r”);

tail_record_length = strlen(buffer_manual);

output = fwrite(buffer_manual, sizeof(char), tail_record_length, o_file_stream);

lr_output_message (“Successfully closed a result file”);

}
if (fclose(o_file_stream)) {

lr_error_message (“Error closing file %s”, output_filename);

}

return 0;
}

Tags:

7 Comments so far ↓

  • Vishwanath

    i want to replace a particular string in a file.

    My file content is of 4 line like below

    john
    deer
    marry
    california

    i want to replace california with like// or $$ or 666 usign C in load runner

  • Lyman Aliff

    Hurrah! In the end I got a weblog from where I be capable of in fact get useful information concerning my study and knowledge.|

  • Cythia Rocchio

    Have you ever considered creating an ebook or guest authoring on other websites? I have a blog centered on the same ideas you discuss and would love to have you share some stories/information. I know my visitors would enjoy your work. If you’re even remotely interested, feel free to shoot me an email.|

  • Emmy Chavez

    I go to see each day a few websites and information sites to read content, except this web site gives quality based posts.|

  • Myung Benshoof

    Very good information. Lucky me I recently found your blog by chance (stumbleupon). I’ve saved as a favorite for later!|

  • Rodolfo Sorn

    Hi there to every one, the contents present at this website are actually remarkable for people knowledge, well, keep up the good work fellows.|

  • Stephan Groen

    Hi there, I found your web site by the use of Google whilst searching for a related subject, your site came up, it seems to be good. I’ve bookmarked it in my google bookmarks.

Leave a Comment