Is there an easy or standard way to parse a comma delimited stirng into a character array?
Printable View
Is there an easy or standard way to parse a comma delimited stirng into a character array?
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.
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
If I'm understanding correctly, all strtok calls after the first should have NULL as their first parameter.
Use the method posted by Valerie Bradley.
Either process them as they're found, or save/push them into a string array for later.
Since you have posted in VC++ forum, I could suggest you go for CStringT::Tokenize.Quote:
Originally Posted by ;129587
Also, look at this thread.