strtik behavior

Discussion in 'C' started by rajib, May 18, 2007.

  1. rajib

    rajib New Member

    Joined:
    May 18, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    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 */
    
    Can somebody please let me know how to handle this?

    Regards -
    Rajib Roy
     
    Last edited by a moderator: May 18, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You don't show the declarations for the properties arrays. One can't tell.

    As an aid to readability, I'd suggest you read something on indentation styles, pick one, drop your indent level to about 4, and set your "smart indent" preferences so that tabs are converted to the appropriate number of spaces.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice