Sign in to follow this  
Yhonatan

Strange C++ strtok Problem

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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 );
}


Share this post


Link to post
Share on other sites

#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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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

Sign in to follow this