string seperation, pulling apart string at commas.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Donkeyoz
    New Member
    • Feb 2008
    • 2

    string seperation, pulling apart string at commas.

    Hi all,

    Trying to do something fairly basic but am getting caught up on it. I am trying to extract text from a .txt file into an array line by line and then dissect that line into various parts, each part separated by a comma and placed into a different variable.
    ie.
    0001,dog,3,five ,123
    0002,cat,7,four ,985
    0003,pig,2,six_ ,435
    0005,cow,4,nine ,999

    so when i dissect it, it comes out as:
    four_num = 0001
    animal = dog
    one_num = 3
    wordnum = five
    three_num = 123

    I have done this the long way, character by character in the array pulling it out and placing it in another variable. But this is long and clunky and requires it to be spot on with no flexibility.

    What I hope to achieve is to be able to search until next comma and place all data before that into the variable (ie. four_num)

    I have tried doing this a few ways, various looping methods, setting counters to count the number of commas that have been detected in the line buffer - to no avail.

    I am trying to avoid as many prebuilt functions as I can as this is for an embedded project that it sort of memory sparse.

    Code:
    		 if(b<21)			// 21 good length for config.txt format, no cutting - reads in full. 
    		 {					//	inc line termination chars.
    		 tempstring[b]=c;
    		 b++; 	 
    		 }
    		 
    		 if(b==21)	// has read off 1st 10 chars and now can under go various condition tests
    		 {			// buffer full string length ten
    		 
    		 for(b=0;b<21;b++)
    	 	 ushell_putchar(tempstring[b]);
    	  	 
    		 b=0;
    
    		 for(b=0;b<21;b++)
    		 {
    		 
    		 if(tempstring[b]==',')
    		 {
    		 com_cnt++;
    		 }
    	 		
    		 if(tempstring[b]!=',' )
    		 {
    		 str_comm[b]=tempstring[b];
    		 ushell_putchar(str_comm[b]);		// parsed to commerless string --> what to do with?
    		 }
    		 }
    		 b=0;
    		 com_cnt=0;
    so, though its badly commented- I think this actually doesnt have the section which pulls apart the line into its segments using something like:-

    if(com_cnt==1)
    four_num[b]=tempstring[b];

    I have got it in the bottom section of code there to pull all the commas out and then print the new comma-less string out however.
    I am using WINGCC with avr studio 4.13 - no real platform issues that will come up though, its a matter of the correct algorithm

    Any help will be much appreciated! thank you all in advance!
    I know tis going to be so easy and brain dead when I see it, but till then...
    Last edited by Banfa; Feb 21 '08, 11:16 AM. Reason: Note that , is a comma not a comer
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You need a thing called a finite state automoton. This is a programming design that breaks a process into states and then writes functions for the individual states.

    There is an article in the C/C++ HowTos on the State Design Pattern. You might read that. A string parser, such as you are doing, is the exanmple of the article and it shows complete code.

    Comment

    • Donkeyoz
      New Member
      • Feb 2008
      • 2

      #3
      thanks for the reply,

      Figured my way was a bit messy.
      I did have it using switch /case 'state machine' style for a while, but it was still messy and clunky.
      anyway, have now written my though agricultural, string function to handle this, which copies a section of string between two points.
      Code:
      char mycommacount(char tempstring[])
      {      
           for(b=0;b<LINE_LENGTH;b++)					// finds the positions of the comers. 
      	   {
                  if(tempstring[b]!=Command_Separator)
                  del_cnt++;
                  if(tempstring[b]==Command_Separator)
                  {
                  delimit[delpos]=del_cnt;
                  delpos++;
                  del_cnt++;
                  }
                  if(tempstring[b]==End_Line_char)
                  {
                  delimit[delpos]=del_cnt-1;
                  return delpos;
                  }
                  
            }  
            return 0;  
      }
      a lot easier!...now converting that function to return the pointer to the string.. cleaning it up further... tips on that issue :P??

      Thanks again, lots of very useful information on this website, don't know how I haven't managed to use it before.

      Comment

      • hsn
        New Member
        • Sep 2007
        • 237

        #4
        simply after reading a line put it in a loop and read char by char until every comma comes in the way

        Comment

        Working...