Split C string into tokens using sscanf

匆匆过客 提交于 2019-11-29 10:50:18

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);

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.

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!