{"id":946,"date":"2010-03-11T12:08:00","date_gmt":"2010-03-11T04:08:00","guid":{"rendered":""},"modified":"2013-11-24T20:43:10","modified_gmt":"2013-11-24T12:43:10","slug":"sscanf%ef%bc%8cscanf%ef%bc%8cfscanf%e4%b8%8e%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/946.html","title":{"rendered":"sscanf\uff0cscanf\uff0cfscanf\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"<p>\u8868\u5934\u6587\u4ef6 #include(stdio.h )<\/p>\n<p>\u5b9a\u4e49\u51fd\u6570 int sscanf (const char *str,const char * format,&#8230;&#8230;..);<br \/>\n\u51fd\u6570\u8bf4\u660e sscanf()\u4f1a\u5c06\u53c2\u6570str\u7684\u5b57\u7b26\u4e32\u6839\u636e\u53c2\u6570format\u5b57\u7b26\u4e32\u6765\u8f6c\u6362\u5e76\u683c\u5f0f\u5316\u6570\u636e\u3002\u683c\u5f0f\u8f6c\u6362\u5f62\u5f0f\u8bf7\u53c2\u8003scanf()\u3002\u8f6c\u6362\u540e\u7684\u7ed3\u679c\u5b58\u4e8e\u5bf9\u5e94\u7684\u53c2\u6570\u5185\u3002<br \/>\n\u8fd4\u56de\u503c \u6210\u529f\u5219\u8fd4\u56de\u53c2\u6570\u6570\u76ee\uff0c\u5931\u8d25\u5219\u8fd4\u56de-1\uff0c\u9519\u8bef\u539f\u56e0\u5b58\u4e8eerrno\u4e2d\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n# include &lt; stdio. h&gt;\r\nint main( )\r\n{\r\n      const char * s = &quot;iios\/12DDWDFF@122&quot; ;\r\n      char buf&#x5B; 20] ;\r\n      sscanf ( s, &quot;%*&#x5B;^\/]\/%&#x5B;^@]&quot; , buf ) ;\r\n      printf ( &quot;%s\\n&quot; , buf ) ;\r\n      return 0;\r\n}\r\n<\/pre>\n<p>\u7ed3\u679c\u4e3a: 12DDWDFF<br \/>\nsscanf\u4e0escanf\u7c7b\u4f3c\uff0c\u90fd\u662f\u7528\u4e8e\u8f93\u5165\u7684\uff0c\u53ea\u662f\u540e\u8005\u4ee5\u5c4f\u5e55( stdin ) \u4e3a\u8f93\u5165\u6e90\uff0c\u524d\u8005\u4ee5\u56fa\u5b9a\u5b57\u7b26\u4e32\u4e3a\u8f93\u5165\u6e90\u3002<\/p>\n<p>\u51fd\u6570\u539f\u578b\uff1a<br \/>\nint scanf( const char *format [,argument]&#8230; );<br \/>\n\u5176\u4e2d\u7684format\u53ef\u4ee5\u662f\u4e00\u4e2a\u6216\u591a\u4e2a {%[*] [width] [{h | l | I64 | L}]type | &#8216; &#8216; | &#8216;\\t&#8217; | &#8216;\\n&#8217; | \u975e%\u7b26\u53f7}\uff0c<br \/>\n\u6ce8\uff1a{a|b|c}\u8868\u793aa,b,c\u4e2d\u9009\u4e00\uff0c[d],\u8868\u793a\u53ef\u4ee5\u6709d\u4e5f\u53ef\u4ee5\u6ca1\u6709d\u3002<br \/>\nwidth:\u5bbd\u5ea6\uff0c\u4e00\u822c\u53ef\u4ee5\u5ffd\u7565\uff0c\u7528\u6cd5\u5982\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nconst char sourceStr&#x5B;] = &quot;hello, world&quot;;\r\nchar buf&#x5B;10] = {0};\r\nsscanf(sourceStr, &quot;%5s&quot;, buf); \/\/%5s\uff0c\u53ea\u53d65\u4e2a\u5b57\u7b26\r\ncout &lt;&lt; buf&lt;&lt; endl;\r\n<\/pre>\n<p>\u7ed3\u679c\u4e3a:hello<br \/>\n{h | l | I64 | L}:\u53c2\u6570\u7684size,\u901a\u5e38h\u8868\u793a\u5355\u5b57\u8282size\uff0cI\u8868\u793a2\u5b57\u8282 size,L\u8868\u793a4\u5b57\u8282size(double\u4f8b\u5916),l64\u8868\u793a8\u5b57\u8282size\u3002<br \/>\ntype :\u8fd9\u5c31\u5f88\u591a\u4e86\uff0c\u5c31\u662f%s,%d\u4e4b\u7c7b\u3002<\/p>\n<p>\u7279\u522b\u7684\uff1a<br \/>\n%*[width] [{h | l | I64 | L}]type \u8868\u793a\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u88ab\u8fc7\u6ee4\u6389\uff0c\u4e0d\u4f1a\u5411\u76ee\u6807\u53c2\u6570\u4e2d\u5199\u5165\u503c\u3002\u5982\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nconst char sourceStr&#x5B;] = &quot;hello, world&quot;;\r\nchar buf&#x5B;10] = {0};\r\nsscanf(sourceStr, &quot;%*s%s&quot;, buf); \/\/%*s\u8868\u793a\u7b2c\u4e00\u4e2a\u5339\u914d\u5230\u7684%s\u88ab\u8fc7\u6ee4\u6389\uff0c\u5373hello\u88ab\u8fc7\u6ee4\u4e86\r\ncout &lt;&lt; buf&lt;&lt; endl;\r\n<\/pre>\n<p>\u7ed3\u679c\u4e3a:world<\/p>\n<p>\u652f\u6301\u96c6\u5408\u64cd\u4f5c\uff1a<br \/>\n%[a-z] \u8868\u793a\u5339\u914da\u5230z\u4e2d\u4efb\u610f\u5b57\u7b26\uff0c\u8d2a\u5a6a\u6027(\u5c3d\u53ef\u80fd\u591a\u7684\u5339\u914d)<br \/>\n%[aB&#8217;] \u5339\u914da\u3001B\u3001&#8217;\u4e2d\u4e00\u5458\uff0c\u8d2a\u5a6a\u6027<br \/>\n%[^a] \u5339\u914d\u975ea\u7684\u4efb\u610f\u5b57\u7b26\uff0c\u8d2a\u5a6a\u6027<\/p>\n<p>1. \u5e38\u89c1\u7528\u6cd5\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5f15\u7528\u7247\u6bb5\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\ncharstr&#x5B; 512] = { 0} ;\r\nsscanf( &quot;123456&quot; , &quot;%s&quot; , str) ;\r\nprintf( &quot;str=%s&quot; , str) ;\r\n<\/pre>\n<p>2. \u53d6\u6307\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\u3002\u5982\u5728\u4e0b\u4f8b\u4e2d\uff0c\u53d6\u6700\u5927\u957f\u5ea6\u4e3a4\u5b57\u8282\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5f15\u7528\u7247\u6bb5\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nsscanf( &quot;123456&quot; , &quot;%4s&quot; , str) ;\r\nprintf( &quot;str=%s&quot; , str) ;\r\n<\/pre>\n<p>3. \u53d6\u5230\u6307\u5b9a\u5b57\u7b26\u4e3a\u6b62\u7684\u5b57\u7b26\u4e32\u3002\u5982\u5728\u4e0b\u4f8b\u4e2d\uff0c\u53d6\u9047\u5230\u7a7a\u683c\u4e3a\u6b62\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5f15\u7528\u7247\u6bb5\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nsscanf( &quot;123456abcdedf&quot; , &quot;%&#x5B;^]&quot; , str) ;\r\nprintf( &quot;str=%s&quot; , str) ;\r\n<\/pre>\n<p>4. \u53d6\u4ec5\u5305\u542b\u6307\u5b9a\u5b57\u7b26\u96c6\u7684\u5b57\u7b26\u4e32\u3002\u5982\u5728\u4e0b\u4f8b\u4e2d\uff0c\u53d6\u4ec5\u5305\u542b1\u52309\u548c\u5c0f\u5199\u5b57\u6bcd\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5f15\u7528\u7247\u6bb5\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nsscanf( &quot;123456abcdedfBCDEF&quot; , &quot;%&#x5B;1-9a-z]&quot; , str) ;\r\nprintf( &quot;str=%s&quot; , str) ;\r\n<\/pre>\n<p>5. \u53d6\u5230\u6307\u5b9a\u5b57\u7b26\u96c6\u4e3a\u6b62\u7684\u5b57\u7b26\u4e32\u3002\u5982\u5728\u4e0b\u4f8b\u4e2d\uff0c\u53d6\u9047\u5230\u5927\u5199\u5b57\u6bcd\u4e3a\u6b62\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5f15\u7528\u7247\u6bb5\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nsscanf( &quot;123456abcdedfBCDEF&quot; , &quot;%&#x5B;^A-Z]&quot; , str) ;\r\nprintf( &quot;str=%s&quot; , str) ;\r\n<\/pre>\n<p>\u641c\u96c6\u4e00\u4e9b\u7279\u6b8a\u7528\u6cd5\uff1a<\/p>\n<p>% [ ] \u7684\u7528\u6cd5\uff1a% [ ] \u8868\u793a\u8981\u8bfb\u5165\u4e00\u4e2a\u5b57\u7b26\u96c6\u5408, \u5982\u679c[ \u540e\u9762\u7b2c\u4e00\u4e2a\u5b57\u7b26\u662f\u201d^\u201d\uff0c\u5219\u8868\u793a\u53cd\u610f\u601d\u3002[ ] \u5185\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u662f1\u6216\u66f4\u591a\u5b57\u7b26\u7ec4\u6210\u3002\u7a7a\u5b57\u7b26\u96c6\uff08% [ ] \uff09\u662f\u8fdd\u53cd\u89c4\u5b9a\u7684\uff0c\u53ef\u5bfc\u81f4\u4e0d\u53ef\u9884\u77e5\u7684\u7ed3\u679c\u3002% [ ^ ] \u4e5f\u662f\u8fdd\u53cd\u89c4\u5b9a\u7684\u3002<\/p>\n<p>% [ a- z] \u8bfb\u53d6\u5728 a- z \u4e4b\u95f4\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u4e0d\u5728\u6b64\u4e4b\u524d\u5219\u505c\u6b62\uff0c\u5982<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nchar s&#x5B; ] = &quot;hello, my friend\u201d ; \/\/ \u6ce8\u610f: ,\u9017\u53f7\u5728\u4e0d a-z\u4e4b\u95f4\r\nsscanf( s, \u201c%&#x5B;a-z]\u201d, string ) ; \/\/ string=hello\r\n<\/pre>\n<p>%[^a-z] \u8bfb\u53d6\u4e0d\u5728 a-z \u4e4b\u95f4\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u78b0\u5230a-z\u4e4b\u95f4\u7684\u5b57\u7b26\u5219\u505c\u6b62\uff0c\u5982<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nchar s&#x5B;]=&quot; HELLOkitty\u201d ; \/\/ \u6ce8\u610f: ,\u9017\u53f7\u5728\u4e0d a-z\u4e4b\u95f4\r\nsscanf ( s, \u201c% &#x5B; ^ a- z] \u201d, string ) ; \/\/ string=HELLO\r\n<\/pre>\n<p>% * [ ^ = ] \u524d\u9762\u5e26 * \u53f7\u8868\u793a\u4e0d\u4fdd\u5b58\u53d8\u91cf\u3002\u8df3\u8fc7\u7b26\u5408\u6761\u4ef6\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nchar s&#x5B; ] = &quot;notepad=1.0.0.1001&quot; ;\r\nchar szfilename &#x5B; 32] = &quot;&quot; ;\r\nint i = sscanf ( s, &quot;%*&#x5B;^=]&quot; , szfilename ) ;\r\n\/\/ szfilename=NULL,\u56e0\u4e3a\u6ca1\u4fdd\u5b58\r\nint i = sscanf ( s, &quot;%*&#x5B;^=]=%s&quot; , szfilename ) ;\r\n\/\/ szfilename=1.0.0.1001\r\n<\/pre>\n<p>% 40c \u8bfb\u53d640\u4e2a\u5b57\u7b26<\/p>\n<p>% [ ^ = ] \u8bfb\u53d6\u5b57\u7b26\u4e32\u76f4\u5230\u78b0\u5230\u2019= \u2019\u53f7\uff0c\u2019^\u2019\u540e\u9762\u53ef\u4ee5\u5e26\u66f4\u591a\u5b57\u7b26, \u5982\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nchar s&#x5B; ] = &quot;notepad=1.0.0.1001&quot; ;\r\nchar szfilename &#x5B; 32] = &quot;&quot; ;\r\nint i = sscanf ( s, &quot;%&#x5B;^=]&quot; , szfilename ) ;\r\n\/\/ szfilename=notepad\r\n<\/pre>\n<p>\u5982\u679c\u53c2\u6570\u683c\u5f0f\u662f\uff1a% [ ^ = : ] \uff0c\u90a3\u4e48\u4e5f\u53ef\u4ee5\u4ece notepad: 1. 0. 0. 1001\u8bfb\u53d6notepad<\/p>\n<p>\u5e94\u7528\u5b9e\u4f8b\uff1aRinex\u661f\u5386\u6570\u636e\u8bfb\u5165<br \/>\n\u4ee5\u4e0b\u662fRinex\u661f\u5386\u6570\u636e\u7684\u7247\u65ad\uff1a<br \/>\n2 10 1 16 2 0 0.0 2.159508876503D-04 4.320099833421D-12 0.000000000000D+00<br \/>\n9.500000000000D+01 1.165625000000D+01 5.293791936254D-09-3.076667279839D+00<br \/>\n7.841736078262D-07 9.284008061513D-03 4.578381776810D-06 5.153754629135D+03<br \/>\n5.256000000000D+05-1.527369022369D-07 1.733543031426D+00 1.303851604462D-08<br \/>\n9.397921470752D-01 2.780625000000D+02 2.941268779788D+00-8.269273019842D-09<br \/>\n1.892935991239D-10 1.000000000000D+00 1.566000000000D+03 0.000000000000D+00<br \/>\n2.000000000000D+00 0.000000000000D+00-1.722946763039D-08 9.500000000000D+01<br \/>\n5.184000000000D+05<br \/>\n\u89c2\u5bdf\u53ef\u4ee5\u53d1\u73b0\uff0c\u6709\u7684\u6570\u636e\u8fde\u5728\u4e86\u4e00\u8d77\uff0c\u6ca1\u6709\u7a7a\u683c\u5206\u9694\uff0c\u5982\u7b2c\u4e8c\u884c5.293791936254D-09-3.076667279839D+00\u3002\u8fd9\u65f6\u5e94\u8be5\u5982\u4f55\u63d0\u53d6\u51fa\u76f8\u5e94\u7684\u6570\u636e\u5462\uff1f\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;stdafx.h&gt;\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint _tmain(int argc, _TCHAR* argv&#x5B;])\r\n{\r\n int wn,year,month,day,hour,prn,minute;\r\n float second;\r\n char t1&#x5B;30],t2&#x5B;30],t3&#x5B;30],t4&#x5B;30],t5&#x5B;30],temp&#x5B;10];\r\n double f1,f2,f3,f4,d1,d2,d3,d4;\r\n FILE *RinexEPP_file;\r\n  if ((RinexEPP_file = fopen(&quot;e:\\data.txt&quot;, &quot;rt&quot;)) == NULL) {\r\n    fprintf(stderr, &quot;Cannot open input file.\\n&quot;);\r\n    exit(1); }\r\n rewind(RinexEPP_file);\r\n if(fscanf(RinexEPP_file,&quot;%d %d %d %d %d %d\\n&quot;,\r\n         &amp;prn,&amp;year,&amp;month,&amp;day,\r\n         &amp;hour,&amp;minute)==EOF) return 1;\r\n fscanf(RinexEPP_file,&quot;%4f&quot;,&amp;second);\r\n fscanf(RinexEPP_file,&quot;%&#x5B;^D]&quot;,t1);  \/\/\u8bfb\u5230D\u5b57\u7b26\u4e3a\u6b62\r\n fscanf(RinexEPP_file,&quot;%4s&quot;,temp);  \/\/\u622a\u53d6D\u540e\u9762\u7684\u56db\u4e2a\u5b57\u7b26\r\n strcat(t1,temp);                   \/\/\u5c06D\u524d\u540e\u4e24\u6bb5\u76f8\u8fde\r\n d1=atof(t1);\r\n d2=atof(t2);\r\n d3=atof(t3);\r\n d4=atof(t4);\r\n}\r\n<\/pre>\n<p>\u5c06\u5176\u6574\u7406\u6210\u4e00\u4e2a\u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nchar *myscanf(FILE *RinexEPP_file);\r\n\r\nchar *myscanf(FILE *RinexEPP_file)\r\n{\r\n char t1&#x5B;30],temp&#x5B;10];\r\n fscanf(RinexEPP_file,&quot;%&#x5B;^D]&quot;,t1);  \/\/\u4e00\u76f4\u8bfb\u5230\u4e3aD\u5b57\u7b26\u4e3a\u6b62\r\n fscanf(RinexEPP_file,&quot;%4s&quot;,temp);\r\n strcat(t1,temp);\r\n return t1;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8868\u5934\u6587\u4ef6 #include(stdio.h ) \u5b9a\u4e49\u51fd\u6570 int sscanf (const char *st [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-946","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/946","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=946"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/946\/revisions"}],"predecessor-version":[{"id":5171,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/946\/revisions\/5171"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}