{"id":2848,"date":"2016-10-24T22:52:53","date_gmt":"2016-10-24T14:52:53","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2848"},"modified":"2016-10-24T22:52:53","modified_gmt":"2016-10-24T14:52:53","slug":"c%e8%af%ad%e8%a8%80%e7%9a%84-strtok-%e5%92%8c-strsep-%e5%87%bd%e6%95%b0%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2848.html","title":{"rendered":"C\u8bed\u8a00\u7684 strtok \u548c strsep \u51fd\u6570\u7684\u4f7f\u7528"},"content":{"rendered":"<p>=Start=<\/p>\n<h4>\u6b63\u6587\uff1a<\/h4>\n<h5>\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p>\u51fd\u6570\u539f\u578b\uff1a<br \/>\n<code>char *strtok(char *s, const char *delim);<\/code><br \/>\n<code>char *strsep(char **s, const char *delim);<\/code><\/p>\n<p>\u529f\u80fd\uff1astrtok\u548cstrsep\u4e24\u4e2a\u51fd\u6570\u7684\u529f\u80fd\u90fd\u662f\u7528\u6765\u5206\u89e3\u5b57\u7b26\u4e32\u4e3a\u4e00\u7ec4\u5b57\u7b26\u4e32\u3002s\u4e3a\u8981\u5206\u89e3\u7684\u5b57\u7b26\u4e32\uff0cdelim\u4e3a\u5206\u9694\u7b26\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u8fd4\u56de\u503c\uff1a\u4eces\u5f00\u5934\u5f00\u59cb\u7684\u4e00\u4e2a\u4e2a\u5b50\u4e32\uff0c\u5f53\u6ca1\u6709\u5206\u5272\u7684\u5b50\u4e32\u65f6\u8fd4\u56deNULL\u3002<\/p>\n<p>\u76f8\u540c\u70b9\uff1a\u4e24\u8005\u90fd\u4f1a\u6539\u53d8\u6e90\u5b57\u7b26\u4e32\uff0c\u60f3\u8981\u907f\u514d\uff0c\u53ef\u4ee5\u4f7f\u7528strdupa\uff08\u7531allocate\u51fd\u6570\u5b9e\u73b0\uff09\u6216strdup\uff08\u7531malloc\u51fd\u6570\u5b9e\u73b0\uff09\u3002<\/p>\n<p>\u4e0d\u540c\u70b9\uff1astrtok\u51fd\u6570\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\u4f1a\u628as\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5728delim\u4e2d\u51fa\u73b0\u7684\u5b57\u7b26\u66ff\u6362\u4e3aNULL\u3002\u7136\u540e\u901a\u8fc7\u4f9d\u6b21\u8c03\u7528strtok(NULL, delim)\u5f97\u5230\u5404\u90e8\u5206\u5b50\u4e32\u3002strsep\u51fd\u6570\u5c06\u8fd4\u56dedelim\u5206\u9694\u7b26\u524d\u9762\u7684\u5b57\u7b26\u4e32\uff0cs\u5c06\u6307\u5411\u5206\u9694\u7b26\u4e4b\u540e\u7684\u5b57\u7b26\u4e32<\/p>\n<p>\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<pre class=\"lang:default decode:true\" title=\"strtok\">#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint main(void) {\n    char s[] = \"hello, world! welcome to china!\";\n    char delim[] = \" ,!\";\n    printf(\"Original: '%s'\\n\\n\", s);\n\n    char *token;\n    for(token = strtok(s, delim); token != NULL; token = strtok(NULL, delim)) {\n        printf(token);\n        printf(\"+\");\n    }\n    printf(\"\\n\");\n    printf(\"After strsep: '%s'\\n\", s);\n    return 0;\n}\n\n\/*\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a\nOriginal: 'hello, world! welcome to china!'\nhello+world+welcome+china+\nAfter strsep: '%s'\n*\/<\/pre>\n<p>&amp;<\/p>\n<pre class=\"lang:default decode:true\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n\nint main(void)\n{\n  char str[] = \"acdeabx\";\n  char *p = NULL;\n  char delim[] = \"ab\";  \/* delim\u662f\u5206\u9694\u7b26\u7684\u96c6\u5408 *\/\n\n  printf(\"the original str is : %s\\n\\n\", str);\n\n  int in = 0;\n  p = strtok(str, delim);\n  while(p != NULL){\n    printf(\"the character is :%s\\n\", p);\n    printf(\"the str is : %s\\n\", str); \/* \u6e90\u5b57\u7b26\u4e32str\u53d1\u751f\u4e86\u53d8\u5316 *\/\n    p = strtok(NULL, delim);\n  }\n}<\/pre>\n<p>&amp;<\/p>\n<pre class=\"lang:default decode:true \">#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint main(){\n    char str[] = \"2,\u5f20\u4e09,89,99,66\";\n    \/\/ char str[] = \",,2,\u5f20\u4e09,89,99,66\";\n    \/\/ str\u662f\u4e00\u4e2a\u6307\u9488\u5e38\u91cf\uff0c\u800cstrsep\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u9700\u8981\u4e00\u4e2a\u6307\u5411\u6307\u9488\u7684\u6307\u9488\uff0c\u6240\u4ee5\u4e0d\u80fd\u5bf9str\u505a\u53d6\u5730\u5740\u64cd\u4f5c\uff0c\u8fd9\u91cc\u518d\u5b9a\u4e49\u4e00\u4e2a\u6307\u9488\u53d8\u91cf\u5c31\u53ef\u4ee5\u53d6\u5730\u5740\u64cd\u4f5c\u4e86\u3002\u5426\u5219\u4f1a\u51fa\u73b0\u6bb5\u9519\u8bef\u3002\n    char *strv = str;\n    char *token = strsep(&amp;strv, \",\");\n    while(token != NULL){\n        printf(\"%s\\t\", token);\n        token = strsep(&amp;strv, \",\");\n    }\n    printf(\"\\n\");\n    return 0;\n}<\/pre>\n<p>&amp;<\/p>\n<pre class=\"lang:default decode:true \">#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint main(void) {\n    char source[] = \"hello, world! welcome to china!\";\n    char delim[] = \" ,!\";\n    printf(\"Original: '%s'\\n\\n\", source);\n\n    char *s = strdup(source);\n    char *token;\n    for(token = strsep(&amp;s, delim); token != NULL; token = strsep(&amp;s, delim)) {\n        printf(token);\n        printf(\"+\");\n    }\n    printf(\"\\n\\n\");\n    printf(\"After strsep: '%s'\\n\", s);\n    return 0;\n}\n\n\/*\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a\nOriginal: 'hello, world! welcome to china!'\nhello++world++welcome+to+china++\nAfter strsep: '%s'\n*\/<\/pre>\n<p>&amp;<\/p>\n<p>\u4e3a\u4ec0\u4e48\u7528strtok\u65f6\u5b50\u4e32\u4e2d\u95f4\u53ea\u6709\u4e00\u4e2a&#8221;+&#8221;\uff0c\u800cstrsep\u5374\u6709\u591a\u4e2a&#8221;+&#8221;\u5462\uff1f\u6587\u6863\u4e2d\u6709\u5982\u4e0b\u7684\u89e3\u91ca\uff1a<\/p>\n<blockquote>\n<p style=\"padding-left: 30px;\">One difference between strsep and strtok_r is that if the input string contains more than one character from delimiter in a row strsep returns an empty string for each pair of characters from delimiter. This means that a program normally should test for strsep returning an empty string before processing it.<\/p>\n<\/blockquote>\n<p>\u5927\u610f\u662f\uff1a\u5982\u679c\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u6709\u8fde\u7eed\u7684\u591a\u4e2a\u5b57\u7b26\u5c5e\u4e8edelim\uff0c\uff08\u6b64\u4f8bsource\u4e2d\u7684\u9017\u53f7+\u7a7a\u683c\uff0c\u611f\u53f9\u53f7+\u7a7a\u683c\u7b49\u5c31\u662f\u8fd9\u79cd\u60c5\u51b5\uff09\uff0cstrtok\u4f1a\u8fd4\u56deNULL\uff0c\u800cstrsep\u4f1a\u8fd4\u56de\u7a7a\u4e32&#8221;&#8221;\u3002\u56e0\u800c\u6211\u4eec\u5982\u679c\u60f3\u7528strsep\u51fd\u6570\u5206\u5272\u5b57\u7b26\u4e32\u5fc5\u987b\u8fdb\u884c\u8fd4\u56de\u503c\u662f\u5426\u662f\u7a7a\u4e32\u7684\u5224\u65ad\u3002\u8fd9\u4e5f\u5c31\u89e3\u91ca\u4e86strsep\u7684\u4f8b\u5b50\u4e2d\u6709\u591a\u4e2a&#8221;+&#8221;\u7684\u539f\u56e0\u3002<\/p>\n<p>\u6211\u4eec\u5728\u81ea\u5df1\u7684\u7a0b\u5e8f\u4e2d\u6700\u597d\u5c3d\u91cf\u907f\u514d\u4f7f\u7528strtok\uff0c\u8f6c\u800c\u4f7f\u7528strsep\u3002\u56e0\u4e3a\uff1a<\/p>\n<blockquote>\n<p style=\"padding-left: 30px;\">* &#8211; Added strsep() which will replace strtok() soon (<span style=\"color: #ff0000;\"><strong>because strsep() is reentrant and should be faster<\/strong><\/span>). Use only strsep() in new code, please.<\/p>\n<\/blockquote>\n<p>\u5373\uff0cstrsep()\u76f8\u6bd4strtok()\u6765\u8bf4\uff0c\u5b83\u662f\u53ef\u91cd\u5165(reentrant)\u7684\uff08\u66f4\u5b89\u5168\uff09\uff0c\u4e14\u901f\u5ea6\u66f4\u5feb\u3002<\/p>\n<h5>\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/blog.chinaunix.net\/uid-26488891-id-3134863.html\">\u5173\u4e8eLinux C\u4e2d\u51fd\u6570strtok\u4f7f\u7528\u8981\u70b9<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/ljq32\/article\/details\/51484154\">Linux C\u51fd\u6570strtok\u89e3\u6790<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/liuintermilan\/article\/details\/6280816\">\u5173\u4e8e\u51fd\u6570strtok\u548cstrtok_r\u7684\u4f7f\u7528\u8981\u70b9\u548c\u5b9e\u73b0\u539f\u7406\uff08\u4e00\uff09<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/buaa_shang\/article\/details\/8189984\">C\u4e2dstrtok()\u51fd\u6570\u548cstrtok_r()\u51fd\u6570<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/yafeng_jiang\/article\/details\/7109285\">strtok\u548cstrsep\u51fd\u6570\u8be6\u89e3<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/mcgrady_tracy\/article\/details\/8142615\">Linux\u7cfb\u7edf\u5e93\u51fd\u6570\u4e4bstrsep<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u6b63\u6587\uff1a \u53c2\u8003\u89e3\u7b54\uff1a \u51fd\u6570\u539f\u578b\uff1a char *strtok(char *s, const cha [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7],"tags":[100,30,660,661],"class_list":["post-2848","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","tag-c","tag-linux","tag-strsep","tag-strtok"],"views":7960,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2848","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2848"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2848\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}