I have a .txt file which have 3 lines. Each line has n number of values separated by the same delimter.
I am using the following function to read the line in C, parse each line based on the delimiter and store the parsed values of a line into one char *val1[], the second into another char *val2[] and third in another one char *val3[].
The issue is I am able to parse and get the value for the first line. Similarly when I try for the second line, I get the values for val2 but val1 gets overwritten !!!
When I run the entire code, val1 , val 2 and val3 contains values for the 3rd line only. values for val1 and val2 are LOST!!
Following is the code -
Code:
while ( fgets(line, sizeof(line), prop_fp) != NULL )
{
printf("Reading Line....\n\n");
fputs(line, stdout);
if (line_number == 1)
{
/* Get the individual values from the input line for item properties */
printf ("delim %s \n",delim);
sub_string = strtok(line, delim);
item_properties[0] = sub_string;
// item_properties[0] = sub_string;
printf ("1st prop = %s \n", item_properties[0]);
cnt = 1;
while ((sub_string = strtok(NULL, delim)) != NULL)
{
item_properties[cnt] = sub_string;
printf("item_properties[%d]is %s \n",cnt, item_properties[cnt] );
cnt++;
}
/*for (increment =0;increment <cnt; increment ++)
{
item_properties[increment] = value[increment];
printf("value, item_prop -%s %s \n", item_properties[increment], item_properties[increment]);
}*/
printf("INLOOP:Item properties are %s %s %s \n\n", item_properties[0], item_properties[1], item_properties[2]);
}
else if (line_number == 2)
{
/* Get the individual values from the input line for item properties */
printf ("delim %s \n",delim);
sub_str = strtok(line, delim);
printf("INLOOP - after suB-string:Item properties are %s %s %s \n\n", item_properties[0], item_properties[1], item_properties[2]);
item_rev_properties[0] = sub_str;
printf ("1st prop = %s \n", item_rev_properties[0]);
cnt = 1;
printf("INLOOP:Item properties are %s %s %s \n\n", item_properties[0], item_properties[1], item_properties[2]);
while ((sub_str = strtok(NULL, delim)) != NULL)
{
printf("INLOOP:Item properties are %s %s %s \n\n", item_properties[0], item_properties[1], item_properties[2]);
item_rev_properties[cnt] = sub_str;
printf("item_rev_properties[%d]is %s \n",cnt, item_rev_properties[cnt] );
cnt++;
}
/*for (increment =0;increment <cnt; increment ++)
{
item_rev_properties[increment] = value[increment];
printf("value, item_prop -%s %s \n", value[increment], item_rev_properties[increment]);
}*/
printf("INLOOP:Item properties are %s %s %s \n\n", item_properties[0], item_properties[1], item_properties[2]);
printf("INLOOP:Item Rev properties are %s %s %s \n\n", item_rev_properties[0], item_rev_properties[1], item_rev_properties[2]);
printf("INLOOP:Item properties are %s %s %s \n\n", item_properties[0], item_properties[1], item_properties[2]);
}
else if (line_number == 3)
{
/* Get the individual values from the input line for item properties */
printf ("delim %s \n",delim);
sub_string = strtok(line, delim);
doc_rev_properties[0] = sub_string;
printf ("\n 1st prop = %s \n", doc_rev_properties[0]);
cnt = 1;
while ((sub_string = strtok(NULL, delim)) != NULL)
{
doc_rev_properties[cnt] = sub_string;
printf("doc_rev_properties[%d]is %s \n",cnt, doc_rev_properties[cnt] );
cnt++;
}
printf("INLOOP:Item properties are %s %s %s \n\n", doc_rev_properties[0], doc_rev_properties[1], doc_rev_properties[2]);
}
line[0] = '\0';
printf("line at end is null %s \n", line);
line_number++;
} /* End of reading the three lines in the properties file */
Regards -
Rajib Roy

