Split C string into tokens using sscanf

后端 未结 3 1485
既然无缘
既然无缘 2020-12-19 09:37

I\'m trying to split a string into tokens but somewhat recursively. I am trying to parse:

\"content=0&website=Google\"

so that I have a

相关标签:
3条回答
  • 2020-12-19 09:44

    I recommend something similar to the following:

    char t_str[100];
    strncpy(t_str, contents, 100);
    //now strtok() on t_str, original contents will be preserved
    
    0 讨论(0)
  • 2020-12-19 09:51

    Here is a solution that seems to work:

    char *contents = "content=0&website=Google";
    char arg[100] = {0};
    char value[100] = {0};
    sscanf(contents, "%[^&]&%s", arg, value);
    printf("%s\n%s\n", arg, value);
    
    0 讨论(0)
  • 2020-12-19 10:07

    scanf is more primitive than you seem to think — %s will match everything up to the next whitespace. Your best solution is probably to stick with strtok but throw it only content you've strduped from the authoritative original.

    0 讨论(0)
提交回复
热议问题