By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,199 Members | 1,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,199 IT Pros & Developers. It's quick & easy.

string seperation, pulling apart string at commas.

P: 2
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.

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.

Expand|Select|Wrap|Line Numbers
  1.          if(b<21)            // 21 good length for config.txt format, no cutting - reads in full. 
  2.          {                    //    inc line termination chars.
  3.          tempstring[b]=c;
  4.          b++;      
  5.          }
  7.          if(b==21)    // has read off 1st 10 chars and now can under go various condition tests
  8.          {            // buffer full string length ten
  10.          for(b=0;b<21;b++)
  11.           ushell_putchar(tempstring[b]);
  13.          b=0;
  15.          for(b=0;b<21;b++)
  16.          {
  18.          if(tempstring[b]==',')
  19.          {
  20.          com_cnt++;
  21.          }
  23.          if(tempstring[b]!=',' )
  24.          {
  25.          str_comm[b]=tempstring[b];
  26.          ushell_putchar(str_comm[b]);        // parsed to commerless string --> what to do with?
  27.          }
  28.          }
  29.          b=0;
  30.          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:-


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...
Feb 21 '08 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 9,197
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.
Feb 21 '08 #2

P: 2
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.
Expand|Select|Wrap|Line Numbers
  1. char mycommacount(char tempstring[])
  2. {      
  3.      for(b=0;b<LINE_LENGTH;b++)                    // finds the positions of the comers. 
  4.        {
  5.             if(tempstring[b]!=Command_Separator)
  6.             del_cnt++;
  7.             if(tempstring[b]==Command_Separator)
  8.             {
  9.             delimit[delpos]=del_cnt;
  10.             delpos++;
  11.             del_cnt++;
  12.             }
  13.             if(tempstring[b]==End_Line_char)
  14.             {
  15.             delimit[delpos]=del_cnt-1;
  16.             return delpos;
  17.             }
  19.       }  
  20.       return 0;  
  21. }
a lot easier! 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.
Feb 24 '08 #3

P: 237
simply after reading a line put it in a loop and read char by char until every comma comes in the way
Feb 25 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.