1

Hello I have a code that is

char * cip = "192.168.0.1\t\t78.90.56.4";

I want to convert it to

char * ip1= "192.168.0.1";
char * ip2 = "78.90.56.4";

I have done

 ip1= strtok(cip, "\t\t ");

I don't know how to get the second ip

I don't know how to do this using string tokenizer. kindly help me thanks

5 Answers 5

5

strtok attempts to modify whatever string you pass to it. Modifying a string literal causes undefined behavior. Since you're passing a string literal to strtok, your code has undefined behavior.

I'd probably do something like this:

std::istringstream cip(""192.168.0.1\t\t78.90.56.4");

std::string ip1, ip2;

std::getline(cip, ip1, '\t');
std::getline(cip, ip2);

Or, since the addresses are separated by the only white-space in the string, you could just use:

cip >> ip1 >> ip2;

Either way, this has defined behavior. As a bonus, it doesn't cause a problem when/if you use multiple threads (which strtok often does).

0
2

Try this function:

ip2 = strtok(NULL, "\t\t");
0
1

http://www.cplusplus.com/reference/clibrary/cstring/strtok/

char * strtok ( char * str, const char * delimiters );

Split string into tokens A sequence of calls to this function split str into tokens, which are sequences of contiguous characters separated by any of the characters that are part of delimiters.

On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of last token as the new starting location for scanning.

To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token.

This end of the token is automatically replaced by a null-character by the function, and the beginning of the token is returned by the function.

Once the terminating null character of str has been found in a call to strtok, all subsequent calls to this function with a null pointer as the first argument return a null pointer.

Parameters

str C string to truncate. The contents of this string are modified and broken into smaller strings (tokens). Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended. delimiters C string containing the delimiters. These may vary from one call to another.

Return Value A pointer to the last token found in string. A null pointer is returned if there are no tokens left to retrieve.

..... to be continue ....

/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
   char str[] ="- This, a sample string.";
   char * pch;
   printf ("Splitting string \"%s\" into tokens:\n",str);
   pch = strtok (str," ,.-");
   while (pch != NULL)
   {
     printf ("%s\n",pch);
     pch = strtok (NULL, " ,.-");
   }
   return 0;
}  
1
  • for (char* pch = strtok(str, " ,.-"); pch != NULL; pch = strtok(NULL, " ,.-")) { /* todo */ } Commented Oct 29, 2013 at 20:38
1

You can pass NULL to strtok_s to get the second item:

char * context=NULL;
char cip[100] = "192.168.0.1\t\t78.90.56.4";
char * ip1= strtok_s(cip,"\t\t",&context);
char * ip2=  strtok_s(NULL,"\t\t",&context);
0

See the example at the bottom of this page. Basically, you make a second call to strtok with NULL in place of the first char * parameter. The delimiters are all packaged up in that second parameter, so I think having "\t\t" is redundant, you might need an additional call of strtok to clear the second '\t'.

2
  • sir its not working ? :( and also what to write i place of second aurgument??
    – tariq
    Commented Mar 27, 2011 at 7:50
  • Try how Mark has written it above. He's used the '\t\t'.The second parameter still is the delimiters.
    – jonsca
    Commented Mar 27, 2011 at 7:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.