Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / void explode(vector<char*>* storage, char* string, char divider)

Author
Message
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 8th Jul 2008 02:23 Edited at: 8th Jul 2008 06:20
FINAL:



Required Headers:



Usage:

Quote: "
std::vector<char*>* storageVec;
explode(&storageVec, "Your|String|Goes|Here", "|");
"


Results:

Quote: "
storageVec.at(0) = "Your";
storageVec.at(1) = "String";
storageVec.at(2) = "Goes";
storageVec.at(3) = "Here";
"


Thanks to everyone that helped.

Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 8th Jul 2008 02:29
Yep, I killed the cat. I can't see what I did wrong, either. Any ideas?

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 8th Jul 2008 04:13 Edited at: 8th Jul 2008 04:15
You can't do this:



Because it is comparing an integer (more specifically, a char) with a char pointer. There might be other flaws (too busy and tired to check right this moment) but this is the one I particularly wanted to point out since there are a fair few people that don't understand how you do string assignment and comparison in C/C++. You can't just do:



Use strcpy to copy a string to another, and strcmp to compare strings, like so:



Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 8th Jul 2008 05:06
So, this?



Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 8th Jul 2008 05:08
Heck, can someone just write their own and show me how it's done?

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 8th Jul 2008 05:23
I didn't see where you said what you're doing, but it looks as if you're trying to parse a string that uses a certain character into a vector of pointers to the sub-strings between the "dividers." Is that correct? If so, what's happening or not happening? Are you getting an error on compile? Not getting the data you expect?

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 8th Jul 2008 05:39 Edited at: 8th Jul 2008 05:41


Note that you have to handle the deallocation of the strings stored in storage yourself if you are going to use char*. Oh, and if you simply want an existing function that does this use strtok.

dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 8th Jul 2008 05:40
I'd assume he's trying to explode a string, similar to how you can in lua. Where you output a table/or vector in this case of the string sliced up at every occurrence of a certain delimiter. In any case, 'strcmp(string[at], "\0") != 0' this isn't required, all you're doing is checking for the end of the string, or a 0; so you can just compare 'string[at]' to 0. Also your use of strcpy() is incorrect, you aren't feeding it 'char*' but 'char', in this instance you want to use memcpy as strcpy will copy all of the string until it reaches the end(0). The way i'd have done it would be to write the current 'string' character to my 'thisString', starting from 0, if I hit my delimiter then I write 0 to the current 'thisString' position and copy it into my vector(which you also currently aren't doing correctly). Once this happens you'd reset the 'thisString' position and continue until you hit the end.

Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 8th Jul 2008 06:04
Actually, dark coder, PHP is the one with the built in explode() function. But yes, you can write your own in Lua, and I have before.

Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 8th Jul 2008 06:06
Wow, didn't know about strtok. Thanks.

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 8th Jul 2008 06:14 Edited at: 8th Jul 2008 06:18
I'm seeing that he keeps pushing back the address of thisString which, when the function exits, is undefined and likely trashed at some point. I'd traverse the original string and push back the address of the pointer, beginning with the first char, at each byte following the divider while replacing the divider by '/0'. Or do the same thing using, as Benjamin suggested, strtok().

Maybe something close to



Edit: This all depends on the original string remaining intact.

Edit 2: strtok might work faster if it's guaranteed that a divider doesn't exist at the very end of the string or you don't mind an empty string.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office

Login to post a reply

Server time is: 2024-11-20 15:29:45
Your offset time is: 2024-11-20 15:29:45