Thursday, July 9, 2009

Write a parameter to a text file in loadrunner script

char *filename = "c:\\myfilename.txt";
long file_stream;
vuser_init()
{

if ((file_stream = fopen(filename, "a+")) == NULL) //open file in append mode
{
lr_error_message ("Cannot open %s", filename);
return -1;
}


fprintf (file_stream, "%s\n", lr_eval_string("THE PARAMETER IS :{Parameter}")); //Parameter is parameter name to be written in a file

Return 0;

}

vuser_end()
{
fclose(file_stream);//dont forget to close file_stream in vuser_end scetion
return 0;
}

7 comments:

  1. I understand how this writes to the txt file. I am trying to read from the text file and display the data in the output. here is my code.

    char * filename ="c:\\myfilename.dat";
    char fileLine[10];
    char buffer[10];
    long file_stream;
    int count;
    Action()

    {
    fopen(filename, "r");
    if ((file_stream = fopen(filename, "r")) == NULL) {

    lr_error_message ("Cannot open %s", filename);
    return -1;
    }

    {while (feof(filename))
    {fgets(fileLine[10], sizeof(10), filename);
    break;
    }
    }

    fclose(file_stream);

    return 0;
    }

    I don't see output in the replay log:
    Ending action vuser_init.
    Running Vuser...
    Starting iteration 1.
    Starting action Action.
    Ending action Action.
    Ending iteration 1.
    Ending Vuser...
    Starting action vuser_end.
    Ending action vuser_end.
    Vuser Terminated

    ReplyDelete
  2. Figured out. The Code:

    char * filename ="c:\\TeamNames.txt";
    long file;
    char names[10];


    Action()
    {
    fopen(filename, "r");
    if ((file = fopen(filename, "r")) == NULL) {
    lr_error_message ("Cannot open %s", file);
    return -1; }

    while (!feof(file))
    {
    fgets(names, 10, file);

    if (!feof(file)) //if not at the end of file print names
    {
    lr_output_message("%s",lr_eval_string(names));
    }

    }

    fclose(file);

    return 0;

    }

    ReplyDelete
  3. Nice piece of code, exactly what i needed. Thanks!

    ReplyDelete
  4. Hi,
    I have used the same logic in my script to write parameter values into file. But I am getting C interpreter error message in Vuser_end at end of Iteraion. Can you please help me on this.

    Thank you
    Mohan

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Hi, I want to save some value in to a note pad during a load test with HP PC. How can I do that when i dont have the access to LGs

    ReplyDelete
  7. Hi All,
    As part of my requirement, I need to read the values from the DB and then write the data to a *.dat or *.txt file (within the Init section) and use the same file as a parameter in the same script instead of using the lr_db_getvalue since this carries an overhead on the CPU/memory consumption on the lg machine and also because we will not be able to allocate block if we use DB to get the values as ther could be a situation wherein the sane data is picked up by different users during the test. Any suggestion how to overcome this?

    ReplyDelete