{"id":198546,"date":"2024-11-14T20:09:06","date_gmt":"2024-11-14T12:09:06","guid":{"rendered":"https:\/\/server.hk\/cnblog\/198546\/"},"modified":"2024-11-14T20:09:07","modified_gmt":"2024-11-14T12:09:07","slug":"%e3%80%8c%e6%95%99%e7%a8%8b%e3%80%8dlinuxc%e4%b8%ad%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%86%e5%89%b2%e7%9a%84%e7%b0%a1%e5%96%ae%e6%96%b9%e6%b3%95-linuxc%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%86%e5%89%b2","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/198546\/","title":{"rendered":"\u300c\u6559\u7a0b\u300dLinuxC\u4e2d\u5b57\u7b26\u4e32\u5206\u5272\u7684\u7c21\u55ae\u65b9\u6cd5 (linuxc\u5b57\u7b26\u4e32\u5206\u5272)"},"content":{"rendered":"<h1 id=\"%e3%80%8c%e6%95%99%e7%a8%8b%e3%80%8dlinuxc%e4%b8%ad%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%86%e5%89%b2%e7%9a%84%e7%b0%a1%e5%96%ae%e6%96%b9%e6%b3%95-linuxc%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%86%e5%89%b2-HAyYhqqvBj\">\u300c\u6559\u7a0b\u300dLinuxC\u4e2d\u5b57\u7b26\u4e32\u5206\u5272\u7684\u7c21\u55ae\u65b9\u6cd5 (linuxc\u5b57\u7b26\u4e32\u5206\u5272)<\/h1>\n<p>\u5728Linux C\u7de8\u7a0b\u4e2d\uff0c\u5b57\u7b26\u4e32\u8655\u7406\u662f\u4e00\u500b\u5e38\u898b\u7684\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u89e3\u6790\u548c\u5206\u6790\u6578\u64da\u6642\u3002\u5b57\u7b26\u4e32\u5206\u5272\u662f\u5c07\u4e00\u500b\u5b57\u7b26\u4e32\u6839\u64da\u7279\u5b9a\u7684\u5206\u9694\u7b26\u5207\u5272\u6210\u591a\u500b\u5b50\u5b57\u7b26\u4e32\u7684\u904e\u7a0b\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u4ecb\u7d39\u5e7e\u7a2e\u5728Linux C\u4e2d\u5be6\u73fe\u5b57\u7b26\u4e32\u5206\u5272\u7684\u7c21\u55ae\u65b9\u6cd5\uff0c\u4e26\u63d0\u4f9b\u76f8\u61c9\u7684\u4ee3\u78bc\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8strtok%e5%87%bd%e6%95%b8-HAyYhqqvBj\">\u4f7f\u7528strtok\u51fd\u6578<\/h2>\n<p>\u5728C\u8a9e\u8a00\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u5206\u5272\u51fd\u6578\u662f<code>strtok<\/code>\u3002\u9019\u500b\u51fd\u6578\u53ef\u4ee5\u6839\u64da\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c07\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u500b\u90e8\u5206\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>strtok<\/code>\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u5305\u542b\u5fc5\u8981\u7684\u6a19\u982d\u6587\u4ef6\u3002<\/li>\n<li>\u7136\u5f8c\uff0c\u4f7f\u7528<code>strtok<\/code>\u51fd\u6578\u9032\u884c\u5206\u5272\u3002<\/li>\n<li>\u6700\u5f8c\uff0c\u904d\u6b77\u6240\u6709\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ol>\n<h3 id=\"%e4%bb%a3%e7%a2%bc%e7%a4%ba%e4%be%8b-HAyYhqqvBj\">\u4ee3\u78bc\u793a\u4f8b<\/h3>\n<pre><code>\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint main() {\n    char str[] = \"Linux,C,\u5b57\u7b26\u4e32,\u5206\u5272,\u793a\u4f8b\";\n    const char s[2] = \",\";\n    char *token;\n\n    \/* \u7372\u53d6\u7b2c\u4e00\u500b\u5b50\u5b57\u7b26\u4e32 *\/\n    token = strtok(str, s);\n\n    \/* \u7e7c\u7e8c\u7372\u53d6\u5176\u4ed6\u5b50\u5b57\u7b26\u4e32 *\/\n    while( token != NULL ) {\n        printf(\"%sn\", token);\n        token = strtok(NULL, s);\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5c07\u5b57\u7b26\u4e32<code>\"Linux,C,\u5b57\u7b26\u4e32,\u5206\u5272,\u793a\u4f8b\"<\/code>\u6839\u64da\u9017\u865f\u9032\u884c\u5206\u5272\u3002\u6bcf\u6b21\u8abf\u7528<code>strtok<\/code>\u51fd\u6578\u6642\uff0c\u5b83\u6703\u8fd4\u56de\u4e0b\u4e00\u500b\u5b50\u5b57\u7b26\u4e32\uff0c\u76f4\u5230\u6c92\u6709\u66f4\u591a\u7684\u5b50\u5b57\u7b26\u4e32\u53ef\u4f9b\u8fd4\u56de\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8strchr%e5%87%bd%e6%95%b8-HAyYhqqvBj\">\u4f7f\u7528strchr\u51fd\u6578<\/h2>\n<p>\u53e6\u4e00\u7a2e\u65b9\u6cd5\u662f\u4f7f\u7528<code>strchr<\/code>\u51fd\u6578\u4f86\u67e5\u627e\u5206\u9694\u7b26\u7684\u4f4d\u7f6e\uff0c\u7136\u5f8c\u624b\u52d5\u9032\u884c\u5206\u5272\u3002\u9019\u7a2e\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u9748\u6d3b\u6027\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u81ea\u5b9a\u7fa9\u5206\u5272\u908f\u8f2f\u6642\u3002<\/p>\n<h3 id=\"%e4%bb%a3%e7%a2%bc%e7%a4%ba%e4%be%8b-HAyYhqqvBj\">\u4ee3\u78bc\u793a\u4f8b<\/h3>\n<pre><code>\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nvoid split_string(const char *str, char delimiter) {\n    const char *start = str;\n    const char *end;\n\n    while ((end = strchr(start, delimiter)) != NULL) {\n        printf(\"%.*sn\", (int)(end - start), start);\n        start = end + 1;\n    }\n    printf(\"%sn\", start); \/\/ \u6253\u5370\u6700\u5f8c\u4e00\u500b\u5b50\u5b57\u7b26\u4e32\n}\n\nint main() {\n    char str[] = \"Linux,C,\u5b57\u7b26\u4e32,\u5206\u5272,\u793a\u4f8b\";\n    split_string(str, ',');\n    return 0;\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b<code>split_string<\/code>\u51fd\u6578\uff0c\u8a72\u51fd\u6578\u63a5\u53d7\u4e00\u500b\u5b57\u7b26\u4e32\u548c\u4e00\u500b\u5206\u9694\u7b26\uff0c\u7136\u5f8c\u4f7f\u7528<code>strchr<\/code>\u67e5\u627e\u5206\u9694\u7b26\u7684\u4f4d\u7f6e\uff0c\u4e26\u6253\u5370\u51fa\u6bcf\u500b\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e6%ad%a3%e5%89%87%e8%a1%a8%e9%81%94%e5%bc%8f-HAyYhqqvBj\">\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f<\/h2>\n<p>\u5982\u679c\u9700\u8981\u66f4\u8907\u96dc\u7684\u5206\u5272\u908f\u8f2f\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u3002C\u8a9e\u8a00\u7684\u6a19\u6e96\u5eab\u4e0d\u76f4\u63a5\u652f\u6301\u6b63\u5247\u8868\u9054\u5f0f\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528POSIX\u6b63\u5247\u8868\u9054\u5f0f\u5eab\u4f86\u5be6\u73fe\u3002<\/p>\n<h3 id=\"%e4%bb%a3%e7%a2%bc%e7%a4%ba%e4%be%8b-HAyYhqqvBj\">\u4ee3\u78bc\u793a\u4f8b<\/h3>\n<pre><code>\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n#include &lt;regex.h&gt;\n\nvoid split_string_regex(const char *str, const char *pattern) {\n    regex_t regex;\n    regmatch_t pmatch[1];\n    const char *start = str;\n    const char *end;\n\n    regcomp(&amp;regex, pattern, REG_EXTENDED);\n    \n    while (regexec(&amp;regex, start, 1, pmatch, 0) == 0) {\n        printf(\"%.*sn\", (int)(pmatch[0].rm_so), start);\n        start += pmatch[0].rm_eo;\n    }\n    printf(\"%sn\", start); \/\/ \u6253\u5370\u6700\u5f8c\u4e00\u500b\u5b50\u5b57\u7b26\u4e32\n\n    regfree(&amp;regex);\n}\n\nint main() {\n    char str[] = \"Linux,C,\u5b57\u7b26\u4e32,\u5206\u5272,\u793a\u4f8b\";\n    split_string_regex(str, \",\");\n    return 0;\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u4f86\u67e5\u627e\u5206\u9694\u7b26\uff0c\u4e26\u6253\u5370\u51fa\u6bcf\u500b\u5b50\u5b57\u7b26\u4e32\u3002\u9019\u7a2e\u65b9\u6cd5\u7684\u9748\u6d3b\u6027\u4f7f\u5176\u9069\u5408\u65bc\u66f4\u8907\u96dc\u7684\u5b57\u7b26\u4e32\u8655\u7406\u9700\u6c42\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-HAyYhqqvBj\">\u7e3d\u7d50<\/h2>\n<p>\u5728Linux C\u4e2d\uff0c\u5b57\u7b26\u4e32\u5206\u5272\u662f\u4e00\u500b\u5e38\u898b\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u901a\u904e\u591a\u7a2e\u65b9\u6cd5\u5be6\u73fe\uff0c\u5305\u62ec\u4f7f\u7528<code>strtok<\/code>\u3001<code>strchr<\/code>\u548c\u6b63\u5247\u8868\u9054\u5f0f\u3002\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u548c\u7dad\u8b77\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684<code><a href=\"https:\/\/server.hk\">VPS<\/a><\/code>\u89e3\u6c7a\u65b9\u6848\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u9700\u6c42\u3002\u7121\u8ad6\u662f<code><a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a><\/code>\u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Linux C\u4e2d\u5b57\u7b26\u4e32\u5206\u5272\u7684\u7c21\u55ae\u65b9\u6cd5\uff0c\u5b78\u7fd2\u5982\u4f55\u6709\u6548\u8655\u7406\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\uff0c\u63d0\u5347\u7de8\u7a0b\u6280\u80fd\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-198546","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/198546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=198546"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/198546\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=198546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=198546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=198546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}