May 18th, 1999, 11:33 AM
Is there an easy or standard way to parse a comma delimited stirng into a character array?
|
Click to See Complete Forum and Search --> : Parsing Comma Delimted Strings May 18th, 1999, 11:33 AM Is there an easy or standard way to parse a comma delimited stirng into a character array? Valerie Bradley May 18th, 1999, 12:01 PM 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. sum61616 February 24th, 2009, 01:28 PM 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 Lindley February 24th, 2009, 01:51 PM If I'm understanding correctly, all strtok calls after the first should have NULL as their first parameter. Syslock February 25th, 2009, 01:10 AM Use the method posted by Valerie Bradley. Either process them as they're found, or save/push them into a string array for later. ajbharani February 25th, 2009, 01:51 AM 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 (http://msdn.microsoft.com/en-us/library/k4ftfkd2(VS.71).aspx). Also, look at this thread. (http://www.codeguru.com/forum/showthread.php?t=425286) codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |