473,785 Members | 2,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string seperation, pulling apart string at commas.

2 New Member
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 2369
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
Donkeyoz
2 New Member
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 New Member
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
9520
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 else if less than 9. How can I improve this, so before it does the checking remove any duplicates in the string? <? $tmp = explode(',',$your_str); if (count($tmp) > 9) { // do something
22
13778
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. #include<iostream> #include<string> int main(int argc, char *argv) {
5
3245
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 this... I am encountering a string that has the example below: a, b, c. "d,e,f,g", abcdef
47
5022
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
2740
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 keep the integrity of the database. We are parsing roughl 81 files and range in size 1 kb to 65 MB files (Average of 400,000 lines in the larger files). I have written this utility with VB.NET 2003 and when I parse all of the files I run out...
4
1854
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 values out that are separated by commas and the last value. I have no guarantee that there won't be a space before the comma (IE: "0 , 0") they could also be: "0,0" or "0, 0"
10
11950
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 building a new word to add to the array but that seems incredibly wasteful. must be some easy way to do it.
4
1392
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 problem I'm having is that one of the data values has commas in it so its blowing up on the other fields. How can I remove the commas from my string if they exist? example: Getting this BMW, Used, 325C, $19,252.00, Smith
3
2122
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
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.