Highlighted
Absent Member.
Absent Member.
7182 views

C logic for writing output to CSV file in different columns

Jump to solution

Hi C/LoadRunner experts,

 

I am looking for some on C logic which would meet my requirement. I made an attempt but I am not fully successfully. When wiritng output to csv file, the value is replacing the existing header values. 

Here is my code

 

     int i;
    char length[100];
    char * filename = "C:\\test.csv";
    long file;
                
    if ((file = fopen(filename, "w+")) == NULL)     

    {
        lr_output_message("File do not exists %s", filename);
        return -1;
    }
      sprintf(length, "\n%s", lr_eval_string("CS_{Unique}"));
    
    i = fwrite(&length, sizeof(length), 1, file);
    if(i>0)
    {
        lr_output_message("Sucessfully wrote file to %d", i);
        fclose(file);
    }

 

Now the output is just as follows in CSV file ( replaced the headers)

 

CS_1

 

but  this is what I am expecting for

 

BeneficiaryName  BeneficiaryID

CS_1                            0001

 

 

Thanks in advance,

Mani

 

0 Likes
1 Solution

Accepted Solutions
Highlighted
Contributor.. Contributor..
Contributor..

Re: C logic for writing output to CSV file in different columns

Jump to solution

Hello Mani,

 

I rewrote the program to use the strtok function to split each line read from the input file into tokens, left padded the Beneficiary Name with zeros and wrote the result to an output file.

 

token = (char *) strtok (str, separators);

 

Attached is a zipped file for the code.  The zipped file includes the input and output files.

 

Cheers,

 

Laurent Lafosse

View solution in original post

0 Likes
5 Replies
Highlighted
Contributor.. Contributor..
Contributor..

Re: C logic for writing output to CSV file in different columns

Jump to solution

Hello Mani,

 

If I understand your code, you wish to have 2 values per line in the CSV file

So you create another variable called Beneficiary ID and append it your output string.

 

char BenefID[12];

int i;

 

/*

 Zero pad the BenefID, assuming that the parameter {Unique} is numeric

Otherwise, use the atoi() funtion

i =atoi(lr_eval_string("{Unique}");

*/

 

sprintf(BenefID, "%03d", lr_eval_string("{Unique}"));

or

sprintf(BenefID, "%03d",i);

 

// Append the BenefID to the string

sprintf(length, "\n%s,%s", lr_eval_string("CS_{Unique}"), BenefID);

 

Cheers,

 

Laurent Lafosse

 

 

0 Likes
Highlighted
Absent Member.
Absent Member.

Re: C logic for writing output to CSV file in different columns

Jump to solution

Hi Laurent,

 

Thank you so much for your reply. I beg your pardon for not writing my expectations clearly.  My requirement is , for every iteration a new value (unique value) should be inserted in Column1 Row 2 and of my csv.

 

lets's assume below is an csv file I have 

 

                column1                     column2            column3

------------------------------------------------------

Row1    BeneficiaryName  | BeneficiaryID | BenCODE

Row2     CS_1                          |  0001                | STest2

-------------------------------------------------------

 

for every iteration I run the code (for every iteration), cs_1 should only be replaced by new value(unique). I am able to do this with my code but the issue here is the "CS_1" is the only value that is getting inserted to the csv. It is replacing all other rows and columns and just writing the CS_1 in file. ( I assume it is because of "W" , the other values are getting replaced).

 

I am getting the following output in csv( replacing al rows and columns) 

column1                     column2            column3

----------------------------------------------

 

CS_1                           

----------------------------------------------

 

I am expecting as below when I run my code.

 

column1                     column2            column3

----------------------------------------------

BeneficiaryName  | BeneficiaryID | BenCODE

CS_1                           |  0001                | STest2

----------------------------------------------

 

CS_1 should only get replaced.  Say If I run 4 iterations then output should be

 

column1                     column2            column3

----------------------------------------------

BeneficiaryName  | BeneficiaryID | BenCODE

CS_4                           |  0001                | STest2

----------------------------------------------

 

 

 

Thanks in advance,

Mani

0 Likes
Highlighted
Contributor.. Contributor..
Contributor..

Re: C logic for writing output to CSV file in different columns

Jump to solution

Hello Mani,

 

I rewrote the program to use the strtok function to split each line read from the input file into tokens, left padded the Beneficiary Name with zeros and wrote the result to an output file.

 

token = (char *) strtok (str, separators);

 

Attached is a zipped file for the code.  The zipped file includes the input and output files.

 

Cheers,

 

Laurent Lafosse

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

Re: C logic for writing output to CSV file in different columns

Jump to solution

Hi Laurent,

 

Thank you very much.. It solved my issue. Appreciate it.!!!

0 Likes
Highlighted
Contributor.
Contributor.

Re: C logic for writing output to CSV file in different columns

Jump to solution

Hi,

 

I want to write to Column B of a csv file and on every iteration, I want to write it to a new row. When I try to write to the csv file, it overrites everything already present and just write to Cell A1 of the csv.

 

Can you please help?

Thanks!

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.