Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need a function...

Status
Not open for further replies.

Themuppeteer

Programmer
Apr 4, 2001
449
BE
Hello,
lets say I have a char* buffer pointing to something like this:

start\n1\n2\nName\n1\n3\nAnotherName\n2\n1\nMyName\nstop\n

How do I filter the 'names' out easily ? Guess here must be some sort of function in C++ that already does this , or do I have to program it myself ? Greetz,
muppeteer.gif


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

"Those who say they understand chess, understand nothing"

-- Robert HUBNER
 
you have \n as a dilimiter so you can use strspn or other string functions and parse the string for the \n. There are multiple ways to do this but I think the easiest would be to parse the string a character at a time. You know the format of

// Pseudo Grammar
start = start\n#\n#\n
name = name\n#\n#\n
stop = stop\n

a string to parse can be defined as

(1)start followed by (n) names followed by (1) stop

You can parse it a character at a time or write a temporary file you can use getline to handle the parsing for you. You also know you have certain key words like stop and start.

This should get you started. When doing comparisons, dont do them case sensitive unless you are absolutly sure you created the string.

Matt
 
Yeah I already did that,I'm testing it now.. Just tought there would be a premade function to do splitting of strings with a delimiter into an array of strings..
Guess not..

Thnx a lot Zyrenthian for your time and effort! Greetz,
muppeteer.gif


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

"Those who say they understand chess, understand nothing"

-- Robert HUBNER
 
If you want to encorporate reusable code into your application for that solution you could check into the stringtok class in boost.


-pete
 
There is a string tokenize function, strtok but it's not as powerful as the java tokenize... Personally, the way I'd do it is using substr, and replace.

Code:
main()
{
...
  string s = buffer;
  vector<string> STRINGS;
  while(s.size() != 0)
  {
     int pos = s.find(&quot;\n&quot;);
     string s1 = s.substr(0,pos);
     s.replace(0,pos+1,&quot;&quot;);
     STRING.push_back(s1);
  }


It isn't pretty, but it works... There is probably a algorithm in algo.h or algorithm.h that will do this better... but since it's not a lot of code...
 
Hmm... I don't like wheel-reinventing. The previous post is a step backwards. Pete hit the nail right on the head when he suggested boost's tokenizer class. Pay attention to that. It's just as powerful, if not more so, than Java's equivalent. Plus, I wouldn't be surprised if it became part of Standard C++ in the very near future.
 
Thnx for your posts,
meanwhile I've already solved the problem.
I work with a buffer. I've got a function 'read' that reads and returns the first value until the \n, then it copies the complete string starting from just behind the \n over itself.
That works to :)

thnx a lot guys! Greetz,
muppeteer.gif


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

&quot;Those who say they understand chess, understand nothing&quot;

-- Robert HUBNER
 
>> That works to :)

It seems you might not understand the concept of a &quot;forum&quot;. There might be many reasons none of us suggested your technique, but I assure you it wasn’t because we don’t know the technique. One might give pause to the fact that you implemented a solution that was NOT recommend by the group you sought guidance from, but rather devised by the very person who asked for help with the problem to start with.

“But, that’s just my opinion… I could be wrong.”
-pete
 
My understanding of a forum is that when somebody has a problem, he says it and other people do suggestions for solving that problem, and I on my turn will use my knowledge to try to help them if they have problems.
It is still up to that person wether or not he decides to implement them or not. Also sometimes you just can't wait for 2 days for an answer. Actually I never wait for an answer,I always continue searching for a solution, so that I can garantee the positive result. My 'technique' is not wrong I think, as it was the proposal of a collegue of mine (who happens to be one of the best programmers I know) and makes perfectly sense.
But of course, thats just my opinion, I could be wrong...

I thank all the people who reply to my post, and I do give a lot of stars (if thats what you're aiming at), if it was a bit helpful I practically give them away. But here I solved it in another way then proposed, not because I did not appreciate your help, just because I found it while waiting for a solution, and time is money.

So thanks again everybody for your help.

Best Regards,
themuppeteer Greetz,
muppeteer.gif


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

&quot;Those who say they understand chess, understand nothing&quot;

-- Robert HUBNER
 
Robert,

Thanks for the clarification. I can certainly understand the &quot;timeliness&quot; issue.

However you cannot expect us to have understood that without your explanation, given that some replies to your post were on the &quot;same day&quot; and your final response was &quot;6 days later&quot;. ;-)

-pete

 
Palbano,

Robert is not my name, its the name of a great chess player ;)
The answers given on the day itself where from Zyrenthian (who has helped me already a lot on this forum,thnx a lot for that!), I was already working on that then. And the other one was from you. I looked at the link but didn't feel like searching at the whole site as I was already close to a solution then.
My final response was 6 days later because I already said I found a solution, and sill some post were happening so I thought you guys just enjoyed talking about the subject wich happens sometimes and therefore continued the discusison.
Sorry about my post this morning, your post was the first thing I read when I came to work this morning and I didn't had my cup of coffee yet...and strangly, this post is the last thing I'll do to today too cause I'm going home now :)

byebye
Greetz,
muppeteer.gif


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

&quot;Those who say they understand chess, understand nothing&quot;

-- Robert HUBNER
 
>> Robert is not my name

[cannon] [blush]
(pete)

DOH... I knew that!

Thanks for your reply. I took no offense to it but was concerned that I had offended you which was certainly not my intention.

Also i was in no way looking for stars. I don't do this for stars. As my post indicated i was concerned about your implementation not using a reusable solution etc. I understand completely (now) why you have taken that direction.

-pete
 
Boost is cool, and powerful... But there the fact that official Boost libraries cannot be GPL'd or LGPL'd is a BIG reason not to use it. There is a GPL'd implimentation of java... While expierimental, it is still a powerful tool.

I am not a zealot of Java or of opensource code... But I do see a place for both, and there are sometimes causes to re-invent the wheel. The method I suggested is similar (untested) code to what I did in a project. There was a DB (a memory mapped flat file, actually) and passing colon delinated strings to construtors that used a parse function much like that above to create a vector of feilds... It was cheap, it was dirty and it rolled. It took a few seconds to write, and being on the school computer we couldn't have installed boost...

I will be looking at boost, and may begin to use it fairly often, but there are reasons to offer other solutions... Even if they are solutions that aren't as elegant as the spiffiest tool out.
 
jstreich,

I agree with you conceptually. Even so the approach you posted could certainly garner criticism since there would certainly be other implementations that would outperform the code you posted. That issue is one of the primary reasons to use established accepted library code rather than rolling your own.

Of course when it comes to optimization in many situations the reality is “who cares” so it’s not a big deal However in the original users post there is no mention of desiring a “quick easy way that doesn’t’ consider performance”. That would be the reason I would not offer a “It isn't pretty, but it works” solution.

“But, that’s just my opinion… I could be wrong.”
-pete
 
Well, again I think this speaks to the nature of a BBS... The whole point is to post a number of solutions so that the person asking the question can pick a solution that best fits the rest of the application. I offered it up because it works, and often you want a working solution as soon as possible, and then to optimize after.

As for performance sting tokenizing will always be O(n), so the question becomes which is faster to impliement and takes less memory? Granted that the biggest flaw in my approach is that I use a vector that doubles in memory each time it reaches cappacity, but memory is cheap. If you were doing something a bit more advanced than parsing a delinated file then you'd have a point.

Just my 2.0*10^-2 cents.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top