{"id":116,"date":"2015-02-17T17:33:06","date_gmt":"2015-02-17T09:33:06","guid":{"rendered":"http:\/\/sqrt-1.me\/?p=116"},"modified":"2016-11-19T14:59:54","modified_gmt":"2016-11-19T06:59:54","slug":"scanf%e5%87%bd%e6%95%b0%e4%b8%ad%e7%9a%84%e6%89%ab%e6%8f%8f%e9%9b%86","status":"publish","type":"post","link":"https:\/\/sqrt-1.me\/?p=116","title":{"rendered":"scanf\u51fd\u6570\u4e2d\u7684\u626b\u63cf\u96c6"},"content":{"rendered":"<p>ANSI C\u8bed\u8a00\u6807\u51c6\u5411scanf\u51fd\u6570\u589e\u52a0\u4e86\u4e00\u79cd\u65b0\u7279\u6027\uff0c\u53eb\u505a\u626b\u63cf\u96c6\u3002\u5229\u7528\u6b64\u7279\u6027\u53ef\u4ee5\u89e3\u51b3\u4e00\u4e9b\u5904\u7406\u6587\u672c\u65f6\u7684\u68d8\u624b\u95ee\u9898\u3002<\/p>\n<p>\u6700\u5f00\u59cb\u6e90\u4e8e\u6211\u5bf9<a href=\"http:\/\/zh.wikipedia.org\/wiki\/%E9%80%97%E5%8F%B7%E5%88%86%E9%9A%94%E5%80%BC\">CSV\uff08\u9017\u53f7\u5206\u9694\u503c\uff09<\/a>\u6587\u4ef6\u7684\u5904\u7406\u3002\uff08\u672c\u6587\u4e0d\u8003\u8651CSV\u6587\u4ef6\u4e2d\u5f15\u53f7\u548c\u8f6c\u4e49\u5b57\u7b26\u7b49\u5176\u4ed6\u7279\u6027\uff09<\/p>\n<p>\u4f8b\u5982\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801<\/p>\n<pre class=\"lang:c decode:true \" >\r\nchar a[100],b[100];\r\nscanf(\"%s,%s\",a,b);\r\n<\/pre>\n<p>\u5982\u679c\u7528\u5b83\u8bfb\u5165\u4e0b\u9762\u7684\u6570\u636e\uff1a<\/p>\n<p>Alice,Bob<\/p>\n<p>\u4f1a\u76f4\u63a5\u628a\u201cAlice,Bob\u201d\u8d4b\u503c\u7ed9a\u800c\u7ee7\u7eed\u7b49\u5f85\u8f93\u5165b\u3002<\/p>\n<p>\u6211\u5728\u7f51\u4e0a\u67e5\u4e86\u8fd9\u79cd\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6cd5\uff0c\u8f83\u4e3a\u7b80\u4fbf\u7684\u4e00\u79cd\u5c31\u662f\u5229\u7528\u626b\u63cf\u96c6\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u4e0a\u9762\u7684\u95ee\u9898\u53ef\u4ee5\u7528\u8fd9\u6bb5\u4ee3\u7801\u89e3\u51b3\uff1a<\/p>\n<pre class=\"lang:c decode:true \" >\r\nchar a[100],b[100];\r\nscanf(\"%[^,],%s\",a,b);\r\n<\/pre>\n<p>\u5176\u4e2d\u683c\u5f0f\u5316\u5b57\u7b26\u4e32&#8221;%[^,],%s&#8221;\u4e2d\u7684&#8221;%[^,]&#8221;\u8868\u793a\u5339\u914d\u9664\u4e86\u9017\u53f7\u4e4b\u5916\u7684\u5176\u4ed6\u5b57\u7b26\uff0c\u76f4\u5230\u9047\u5230\u9017\u53f7\uff1b&#8221;,&#8221;\u8868\u793a\u8bfb\u5165\u4e00\u4e2a\u9017\u53f7\uff1b&#8221;%s&#8221;\u8868\u793a\u8bfb\u5165\u5269\u4e0b\u7684\u90e8\u5206\u76f4\u5230\u9047\u5230\u7a7a\u767d\u7b26\u3002<\/p>\n<p>\u8fd9\u79cd\u8868\u793a\u683c\u5f0f\u548c<a href=\"http:\/\/zh.wikipedia.org\/wiki\/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F\">\u6b63\u5219\u8868\u8fbe\u5f0f<\/a>\u6709\u4e9b\u76f8\u4f3c\u4e4b\u5904\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4ece<a href=\"http:\/\/flash-gordon.me.uk\/ansi.c.txt\">\u8fd9\u91cc<\/a>\u590d\u5236\u7684\u4e00\u6bb5ANSI C\u6807\u51c6\uff1a<\/p>\n<p>[<br \/>\nMatches a nonempty sequence of characters from a set of expected<br \/>\ncharacters (the scanset ). The corresponding argument shall be a<br \/>\npointer to the initial character of an array large enough to accept<br \/>\nthe sequence and a terminating null character, which will be added<br \/>\nautomatically. The conversion specifier includes all subsequent<br \/>\ncharacters in the format string, up to and including the matching<br \/>\nright bracket ( ] ). The characters between the brackets (the<br \/>\nscanlist ) comprise the scanset, unless the character after the left<br \/>\nbracket is a circumflex ( ^ ), in which case the scanset contains all<br \/>\ncharacters that do not appear in the scanlist between the circumflex<br \/>\nand the right bracket. As a special case, if the conversion specifier<br \/>\nbegins with [] or [^] , the right bracket character is in the scanlist<br \/>\nand the next right bracket character is the matching right bracket<br \/>\nthat ends the specification. If a &#8211; character is in the scanlist and<br \/>\nis not the first, nor the second where the first character is a ^ ,<br \/>\nnor the last character, the behavior is implementation-defined.<\/p>\n<p>\u8fd9\u6bb5\u8bdd\u5927\u610f\u662f\u8bf4\uff0c\u5728\u683c\u5f0f\u5b57\u7b26\u4e32\u4e2d\uff0c\u767e\u5206\u53f7\u540e\u7d27\u8ddf\u7740\u7684\u4e00\u5bf9[]\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u79f0\u4e3a\u626b\u63cf\u96c6\uff0c\u5339\u914d\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u90fd\u53ef\u4ee5\u5728\u626b\u63cf\u96c6\u4e2d\u627e\u5230\u3002\u5982\u679c\u626b\u63cf\u96c6\u4e2d\u7b2c\u4e00\u4e2a\u5b57\u7b26\u662f^\uff0c\u90a3\u4e48\u5c31\u53d6\u53cd\u4e49\uff0c\u8868\u793a\u5b57\u7b26\u4e32\u4e0d\u5305\u542b^\u548c]\u4e4b\u95f4\u7684\u5b57\u7b26\u3002<\/p>\n<p>\u4e0b\u9762\u4e3e\u51e0\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<table>\n<tbody>\n<tr>\n<th>\u8bed\u53e5<\/th>\n<th>\u8f93\u5165<\/th>\n<th>\u7ed3\u679c<\/th>\n<\/tr>\n<tr>\n<td>scanf(&#8220;%[123]&#8221;,a);<\/td>\n<td>11223344<\/td>\n<td>a=&#8221;112233&#8243;<\/td>\n<\/tr>\n<tr>\n<td>scanf(&#8220;%[^123]&#8221;,a);<\/td>\n<td>4321<\/td>\n<td>a=&#8221;4&#8243;<\/td>\n<\/tr>\n<tr>\n<td>scanf(&#8220;%[^d]&#8221;,a);<\/td>\n<td>abcdef<\/td>\n<td>a=&#8221;abc&#8221;<\/td>\n<\/tr>\n<tr>\n<td>scanf(&#8220;%[0-9]%[a-z]%s&#8221;,a,b,c);<\/td>\n<td>456ab789<\/td>\n<td>a=&#8221;456&#8243;<br \/>\nb=&#8221;ab&#8221;<br \/>\nc=&#8221;789&#8243;<\/td>\n<\/tr>\n<tr>\n<td>scanf(&#8220;%[^\\n]&#8221;,a);<\/td>\n<td>hello world<\/td>\n<td>a=&#8221;hello world&#8221;<\/td>\n<\/tr>\n<tr>\n<td>scanf(&#8220;%[^@]@%[^.].%s&#8221;,a,b,c);<\/td>\n<td>someone@example.com<\/td>\n<td>a=&#8221;someone&#8221;<br \/>\nb=&#8221;example&#8221;<br \/>\nc=&#8221;com&#8221;<\/td>\n<\/tr>\n<tr>\n<td>scanf(&#8220;%[^(](%*[^)])%s&#8221;,a,b);<\/td>\n<td>abc(def)ghi<\/td>\n<td>a=&#8221;abc&#8221;<br \/>\nb=&#8221;ghi&#8221;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5728fscanf\u51fd\u6570\u548csscanf\u51fd\u6570\u4e2d\u90fd\u53ef\u4ee5\u4f7f\u7528\u3002<\/p>\n<p>\u626b\u63cf\u96c6\u5728\u5904\u7406\u6587\u672c\u65f6\uff08\u5c24\u5176\u662f\u6839\u636e\u7279\u5f81\u5b57\u7b26\u5206\u5272\u6587\u672c\uff09\u548c\u5224\u65ad\u8f93\u5165\u7684\u5b57\u7b26\u662f\u5426\u6709\u6548\u65f6\uff08\u4f8b\u5982\u8981\u6c42\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u65f6\uff09\u662f\u4e00\u79cd\u7279\u522b\u7b80\u5355\u5b9e\u7528\u7684\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ANSI C\u8bed\u8a00\u6807\u51c6\u5411scanf\u51fd\u6570\u589e\u52a0\u4e86\u4e00\u79cd\u65b0\u7279\u6027\uff0c\u53eb\u505a\u626b\u63cf\u96c6\u3002\u5229\u7528\u6b64\u7279\u6027\u53ef\u4ee5\u89e3\u51b3\u4e00\u4e9b\u5904\u7406\u6587\u672c\u65f6\u7684\u68d8\u624b\u95ee\u9898\u3002 &hellip; <a href=\"https:\/\/sqrt-1.me\/?p=116\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cscanf\u51fd\u6570\u4e2d\u7684\u626b\u63cf\u96c6\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":"","_links_to":"","_links_to_target":""},"categories":[2],"tags":[],"class_list":["post-116","post","type-post","status-publish","format-standard","hentry","category-c-language"],"_links":{"self":[{"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=116"}],"version-history":[{"count":11,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":347,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions\/347"}],"wp:attachment":[{"href":"https:\/\/sqrt-1.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqrt-1.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}