473,408 Members | 2,450 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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.

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.          }
  6.  
  7.          if(b==21)    // has read off 1st 10 chars and now can under go various condition tests
  8.          {            // buffer full string length ten
  9.  
  10.          for(b=0;b<21;b++)
  11.           ushell_putchar(tempstring[b]);
  12.  
  13.          b=0;
  14.  
  15.          for(b=0;b<21;b++)
  16.          {
  17.  
  18.          if(tempstring[b]==',')
  19.          {
  20.          com_cnt++;
  21.          }
  22.  
  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;    
  31.  
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...
Feb 21 '08 #1
3 2345
weaknessforcats
9,208 Expert Mod 8TB
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
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.             }
  18.  
  19.       }  
  20.       return 0;  
  21. }
  22.  
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.
Feb 24 '08 #3
hsn
237 100+
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

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

Similar topics

6
by: Ragnorack67 | last post by:
Hello again, I have one further question about improving a validation. Currently this will validate a string of words separated by commas, and if it's greater than 9 will do something, or something...
22
by: ineedyourluvin1 | last post by:
Hello all! I've been looking for a way to strip characters from strings such as a comma. This would be great for using a comma as a delimiter. I show you what I have right now. ...
5
by: Ann Marinas | last post by:
Happy New Year to all! :D I am currently developoing an application that imports data from a CSV file. Each comma represents an array item that I need to extract data with. My problem is...
47
by: sudharsan | last post by:
could any one please give me a code to reverse a string of more than 1MB .??? Thanks in advance
10
by: klineb | last post by:
Good Day, I have written and utility to convert our DOS COBOL data files to a SQL Server database. Part of the process requires parsing each line into a sql statement and validting the data to...
4
by: sklett | last post by:
given one of these two string: ProtocolRecord(0, 100, 100, arZeroMax , 1, 0, SKIP,0), ProtocolRecord(0, 100, 100, arSetSet , 4, 0, DJMPNZSTOP, (void *) &Protocol_TD) I want to pull all the...
10
by: pantagruel | last post by:
Hi, I'm looking for an optimal javascript function to split a camelcase string and return an array. I suppose one could loop through the string, check if character is uppercase and start...
4
by: igotyourdotnet | last post by:
I have a question. I'm reading a CSV file that is uploading to my SQL db, I'm parsing out the file line by line. I'm getting the values and putting them into an arrayList seperate by commas. The...
3
by: gh | last post by:
I have a string that is delimited with commas. Is there a function in C# that will break apart the string at each comma, so I can copy the value to a var? This is a .net v1.1.4 app. TIA
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.