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

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().

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...