0

Okay, I have a problem. In this code, after the first call to strtok, the variable becomes "developer" when it was originally "developer;developer 0;developer". I would have expected it to become "developer 0;developer" after the first call..

void Con_Execute(char *text)
{
    char *pch;
    char string[10240];

    strcpy(string,text);

    if(!strstr(string,";"))
    {
        Cmd_ExecuteString(string);
        return;
    }

    pch = strtok(string,";");
    while(pch)
    {
        Cmd_ExecuteString(string);
        pch = strtok(NULL,";");
    }
}
2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by tomtetlaw
0

Now I think I see what you're question is:

Try running this:

#include <stdio.h>
#include <string.h>

int main() {
  int i, unique; 
  char str[]="developer,developer0,developer";
  char *pstr;
  printf("\n\n original string: %s \n", str);
  pstr = strtok(str, ",");
  printf("\n  pointer: %s \n", pstr);
  printf("\n string[]: %s \n", str);


  while(1) {
    pstr = strtok(NULL, ",");
    if(!pstr)
      break;
    printf("\n  pointer: %s \n", pstr);
    printf("\n string[]: %s \n", str);
  }
  printf("\nFinal string prints as: %s \n", str);
  printf("\nBut the final string has visible char's of: \n");
  for(i=0;i<sizeof(str);i++)
    putchar(str[i]);
  
  printf("\n\n\t\t\t     press enter when ready");

  i = getchar(); ++i;
  return 0;
}

Edited by Adak: n/a

0

I don't understand, that's exactly what I did, but I only ever get the first 'developer' :(

Edited by tomtetlaw: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.