|
-
May 18th, 1999, 11:33 AM
#1
Parsing Comma Delimted Strings
Is there an easy or standard way to parse a comma delimited stirng into a character array?
-
May 18th, 1999, 12:01 PM
#2
Re: Parsing Comma Delimted Strings
Use the function strtok() to separate the string into substrings with the delimiter character being the comma character.
For example:
char string[] = "This string,is delimited,with,commas.";
char seps[] = ",";
char * token;
token = strtok(string, seps); // Get the first token
while (token)
{
printf("Found a token: %s\n", token);
token = strtok(NULL, seps); // Get the next token
}
Your output should be:
Found a token: This string
Found a token: is delimited
Found a token: with
Found a token: commas.
Hope this helps!
=================================================
Valerie L. Bradley
Software Engineer
Intel Corporation
* All opinions expressed are mine and not those of my employer.
-
February 24th, 2009, 02:28 PM
#3
Re: Parsing Comma Delimted Strings
char string[80]=" ";
char * dummy;
char seps[] = ",";
int k,l,m;
float x,y,z;
//
fscanf(f_ptr1,"%s\n",string); //read a line: 1,1,1,E1,0.13060,-0.40193,-0.90631
k = atol(strtok(string, seps));
l = atol(strtok(string, seps)); // Get the first token
m = atol(strtok(string, seps)); // Get the first token
dummy = strtok(string, seps); // Get the first token
x = atof(strtok(string, seps)); // Get the first token
y = atof(strtok(string, seps)); // Get the first token
z = atof(strtok(string, seps)); // Get the first token
I found these codes can not read correctly and can not parsing correctly. Would you please help to fix the codes
Thanks alots
-
February 24th, 2009, 02:51 PM
#4
Re: Parsing Comma Delimted Strings
If I'm understanding correctly, all strtok calls after the first should have NULL as their first parameter.
-
February 25th, 2009, 02:10 AM
#5
Re: Parsing Comma Delimted Strings
Use the method posted by Valerie Bradley.
Either process them as they're found, or save/push them into a string array for later.
Last edited by Syslock; February 25th, 2009 at 02:13 AM.
-
February 25th, 2009, 02:51 AM
#6
Re: Parsing Comma Delimted Strings
 Originally Posted by ;129587
Is there an easy or standard way to parse a comma delimited stirng into a character array?
Since you have posted in VC++ forum, I could suggest you go for CStringT::Tokenize.
Also, look at this thread.
Rate the posts which you find useful
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
|