Jump to content

How do I read a token into a string? (C++)


Guest silverskin
 Share

Recommended Posts

Guest silverskin

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?

Link to comment
Share on other sites

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 );

  }

}

 

Output

A string   of ,,tokens

and some  more tokens

 

Tokens:

A

string

of

tokens

and

some

more

tokens

Link to comment
Share on other sites

Guest silverskin

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.

Link to comment
Share on other sites

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, "'"); 

 } 

}

Link to comment
Share on other sites

Guest silverskin

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...