|
-
Sep 26th, 2002, 10:03 PM
#1
Thread Starter
Addicted Member
How to parse comma delimited string?
Hi,
I can read a comma delimited file and load the data into a string one line at a time. So, I can get something like...
string s;
s = "12.3, 14.5, 16.7";
I can iterate through the string using s.substr and a 'for' loop...is there an equivalent to 'instr' command as in basic?
Regards,
ChuckB
-
Sep 26th, 2002, 10:06 PM
#2
Frenzied Member
-
Sep 26th, 2002, 10:06 PM
#3
Frenzied Member
Though I believe that the boost library (http://www.boost.org/ ) has a handy string_tokenizer class that will do that work for you =).
Z.
-
Sep 27th, 2002, 09:33 AM
#4
Thread Starter
Addicted Member
Z,
Thanks for the link. They have a lot of samples using iterators...which is what I needed to see. Are you holding out on some other real usefull C++ sites? :-)
Regards,
ChuckB
-
Sep 27th, 2002, 10:08 AM
#5
Frenzied Member
Don't overlook strtok() for char * -
Code:
#include <stdio.h>
#include <string.h>
int main(){
char *whatever=
"this is an, example, showing parsing using, commas and spaces.";
char *p;
p=strtok(whatever,", "); /* find the first space */
while(p){
printf("%s\n",p);
p=strtok('\0'," ,"); /* use spaces & commas*/
}
return 0;
}
-
Sep 27th, 2002, 11:57 AM
#6
Monday Morning Lunatic
Just be bloody careful with where you point strtok -- it changes the string you pass in.
Also, I don't think you can use strtok simultaneously on multiple strings.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 27th, 2002, 04:39 PM
#7
strtok doesn't change the string, but you can't use it on more than one string, that's right. And it is not thread-safe.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 27th, 2002, 04:50 PM
#8
Frenzied Member
strtok behaves differently on different boxes. Parksie is right - on HP UX 11.0, for example, it does change the source string.
strtok_r uses a third argument - a char* - that stops strtok from clobbering the source.
In MSVC++ it does not hurt the source.
-
Sep 27th, 2002, 04:57 PM
#9
Monday Morning Lunatic
Quick aside for anyone else... the "_r" means "reentrant", which basically says that it's possible to call the function twice in the same process, at the same time (such as multiple threads). Reentrant functions don't have any static data, or anything like that.
Well, they could. But they don't cause massive lossage if you do it
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 27th, 2002, 05:27 PM
#10
Another side note: the strtok of MSVC++ in the dynamic or multithreaded static CRT library is thread-safe.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|