简体   繁体   中英

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 testing 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM