Guest silverskin Posted October 9, 2002 Report Share Posted October 9, 2002 Probably not the best forum to ask this question, but anyway... I have written the beginnings of a program, but it doesn't work yet cos I need to read a token into a string variable and I don't know how. Here's my code so far: #include <stdio.h> #include <string.h> int main(void) { char *token; char chartest[] = "<product catagory='100'>Product1</product>"; char tempString; token = strtok(chartest, "'"); while(token != NULL) { token = strtok(NULL, "'"); printf("%sn",token); //prints token to screen sprintf(token, tempString); //doesn't work printf(tempString); //doesn't work token = strtok(NULL, "'"); } } The token should be '100' from the chartest string. I can print it directly to the screen by printf("%sn",token) but I need to save it as a separate string. I thought I'd try the function sprintf which I thought printed to a string insead of to the screen. Can anyone help? Quote Link to comment Share on other sites More sharing options...
Cannonfodder Posted October 9, 2002 Report Share Posted October 9, 2002 haven't really researched this topic or tried this but here's a quick example: Example /* STRTOK.C: In this program, a loop uses strtok * to print all the tokens (separated by commas * or blanks) in the string named "string". */ #include <string.h> #include <stdio.h> char string[] = "A stringtof ,,tokensnand some more tokens"; char seps[] = " ,tn"; char *token; void main( void ) { printf( "%snnTokens:n", string ); /* Establish string and get the first token: */ token = strtok( string, seps ); while( token != NULL ) { /* While there are tokens in "string" */ printf( " %sn", token ); /* Get next token: */ token = strtok( NULL, seps ); } } OutputA string of ,,tokens and some more tokens Tokens: A string of tokens and some more tokens Quote Link to comment Share on other sites More sharing options...
Guest silverskin Posted October 10, 2002 Report Share Posted October 10, 2002 Thanks for the help but... I am able to print the individual tokens straight to the screen already. I need to be able to save the tokens into strings. For example, if the first token is "A", I want to save it as string[] firstToken; So I can later print it to the screen by printf("%s",firstToken); Does that make more sense? Thanks. Quote Link to comment Share on other sites More sharing options...
Cannonfodder Posted October 10, 2002 Report Share Posted October 10, 2002 Check this out.. Instead of using a char* for tempstring, I preallocated the storage. If you want to use a char * you will have to allocate it yourself ahead of time and free it later. Otherwise, this seems to work. I figured it out (been awhile) by googling sprintf() gcc example #include <stdio.h> #include <string.h> int main(void) { char *token; char chartest[] = "<product catagory='100'>Product1</product>"; char tempString[80]; token = strtok(chartest, "'"); while(token != NULL) { // get token and printf() to screen token = strtok(NULL, "'"); printf("Token-> %sn",token); // copy token to tempString sprintf(tempString, "Converted Token -> %s", token); // printf() tempString printf("String Token -> %sn", tempString); // get next token token = strtok(NULL, "'"); } } Quote Link to comment Share on other sites More sharing options...
Guest silverskin Posted October 10, 2002 Report Share Posted October 10, 2002 Wow!!!!!!!!!! that actually works! I can now, finally, get on with the rest of my program. This problem really had me stumped. Thanks a lot. I know who to come back to if I have any further problems :D Quote Link to comment Share on other sites More sharing options...
Cannonfodder Posted October 10, 2002 Report Share Posted October 10, 2002 heh, I have experience as a programmer and with c, but not big time and not on linux. I figured it out by looking for definitions and examples. Feel free to ask though, there are others here who know C also :) Also, interesting enough. PHP has similar function libraries such as sprintf(). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.