开发者

strtok_r tokens with delimiters

I've found similar posts, but no clear answers to my questions about strtok_r.

I'm using strtok_r to parse a command line to get commands I need to execute via execv with flags, however, for testing purposes, I print out. When trying to delimit multiple characters, excluding whitespace, it works fine. But when 开发者_如何学JAVAtesting for whitespace, using the code below:

void tokenize(char *str1)
{
  char *token;
  char *saveptr1;
  int j, i;


  const char *delim = " ";
  i = strlen(str1);

  for(j = 0; j < i; j++, str1 = NULL)
  {
    token = strtok_r(str1, delim, &saveptr1);
    if(token == NULL)
      break;

    printf("save: %s\n", token);
    printf("\n");
  }
}

I get the following output for a test string (ls -al):

save: ls


How do you read the string? Maybe you are reading the string with something like: cin >> string; or scanf("%s", str); that only read the first token("ls").

Instead youd should read the entire line with something like cin.getline() or scanf("%[^\n]", str). Check that!

Why strtok_r istead of strtok?


Your for loop is setting str1=NULL after each time through the loop

for(j = 0; j < i; j++, str1 = NULL)
{
    ...
}

so the first time through the loop, it works as you would expect, but after that, no further tokens are extracted because str1 doesn't point to the string any more.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜