Strange C++ strtok Problem

Started by
6 comments, last by ToohrVyk 16 years, 10 months ago
hello all. i'm using strtok and i got some strange problem. here is my code:

    printf( "Msg came in \"%s\"\n", infoo22 );
   char *result = NULL;
   result = strtok( infoo22, "#" );
   while( result != NULL ) {
       strcpy(tempresult,result);   
       printf( "result is \"%s\"\n", result );
       akel(tempresult);
       result = strtok( NULL, "#" );
   }       

this code gets a string (infoo22) and suppose to split it. each part of it is suppose to work with function akel. an example for my problem: assuming infoo22 is _2_3_4_#_4_5_3_# it print it on the screen but the Result thing writes only _2_3_4_ and i have changed the printf location and it gave me a <null> i have no idea why it gives a null. can anyone please help me? thanks in advance
Advertisement
Quote:From MSDN - strtok
char *strtok( char *strToken, const char *strDelimit );

strToken
String containing token(s).
strDelimit
Set of delimiter characters.

On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok.

Each call to strtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument.


// MSDN Example#include <string.h>#include <stdio.h>char string[] = "A string\tof ,,tokens\nand some  more tokens";char seps[]   = " ,\t\n";char *token;void main( void ){   printf( "%s\n\nTokens:\n", string );   /* Establish string and get the first token: */   token = strtok( string, seps );   while( token != NULL )   {      /* While there are tokens in "string" */      printf( " %s\n", token );      /* Get next token: */      token = strtok( NULL, seps );   }}


[edit - I screwed all sorts of stuff up in the original verison :) ]

Try making a variable out of your "#". So add a char* sep = "#"; and replace all of your "#" with the new variable. That is about the only thing that looks real different from your code and the MSDN example. You might comment out the strcpy and akel calls as well and see if that does anything (just an idea).

[Edited by - Rattrap on June 7, 2007 12:51:40 PM]

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

Other details concerning your code: first, why the additional strcpy? Second, since you mentioned C++:

std::stringstream tokens;tokens << info22;do {  std::string token;  std::getline(tokens,token,'#');  akel(token.c_str());  tokens.ignore(1);}while (tokens);
First of all thanks all for trying to help me.
Secondly, I made some changes in my code.
Still, same thing happens

this is my new code:

  printf( "Msg came in \"%s\"\n", infoo22 );   char *result = NULL;   char *thingt = "#";   result = strtok( infoo22, thingt );   while( result != NULL ) {          strcpy(tempresult,result);        printf( "result is \"%s\"\n", result );       akel(tempresult);       result = strtok( NULL, thingt );   }                            
#define	_CRT_SECURE_NO_WARNINGS#include <string.h>#include <stdio.h>int main(){	char infoo22[] = "_2_3_4_#_4_5_3_#";	char *result = NULL;	const char thingt[] = "#";	result = strtok( infoo22, thingt );	while( result != NULL )	{		//strcpy(tempresult,result); 		printf( "result is \"%s\"\n", result );		//akel(tempresult);		result = strtok( NULL, thingt );	}	return 0;}


This results in

result is "_2_3_4_"
result is "_4_5_3_"

in Microsoft VC 2005. If I change the

char infoo22[] = "_2_3_4_#_4_5_3_#";
to
char* infoo22 = "_2_3_4_#_4_5_3_#";

I get a runtime error.

If I make the same change for thingt, it behaves just fine.

[edit]
PS - Do agree with ToohrVyk's idea to switchto std::string

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

Quote:strtok manual page

BUGS
Avoid using these functions. If you do use them, note that:

- These functions modify their first argument.

- These functions cannot be used on constant strings.

- The identity of the delimiting character is lost.

- The strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you.


'nuff said. You shouldn't use this even in C, let alone in C++, where much better solutions exist.

Hi guys

So i putted this code but now i get an error

This is the code
std::stringstream tokens;tokens << infoo22;do {  std::string token;  std::getline(tokens,token,'#');  akel(token.c_str());  tokens.ignore(1);}while (tokens);


This is the error on akel(..) line.
I suppose to get an array but I get a const char

invalid conversion from `const char*' to `char*' 


what should i do?

thanks in advance

p.s Can anyone explaine me this code?

Another p.s Sorry for my english
Quote:Original post by Yhonatan
This is the error on akel(..) line.
I suppose to get an array but I get a const char


If akel doesn't need to modify its argument, make it take a const char* instead of a char*.

If akel needs to modify its argument, make it take a std::string by value, so that it has its own copy to work on.

This topic is closed to new replies.

Advertisement