{"id":425,"date":"2010-04-23T18:40:51","date_gmt":"2010-04-23T10:40:51","guid":{"rendered":"http:\/\/115.29.108.60\/?p=425"},"modified":"2010-10-07T20:20:30","modified_gmt":"2010-10-07T12:20:30","slug":"c-primer-chapter8","status":"publish","type":"post","link":"http:\/\/115.29.108.60\/?p=425","title":{"rendered":"C++ Primer Chapter8"},"content":{"rendered":"<p>1\u3001IO\u5bf9\u8c61\u4e0d\u53ef\u590d\u5236\u6216\u8d4b\u503c\uff1a\u53ea\u6709\u652f\u6301\u590d\u5236\u7684\u5143\u7d20\u7c7b\u578b\u53ef\u4ee5\u5b58\u50a8\u5728vector\u6216\u5176\u4ed6\u5bb9\u5668\u7c7b\u578b\u91cc\uff1b\u5f62\u53c2\u6216\u8fd4\u56de\u7c7b\u578b\u4e5f\u4e0d\u80fd\u4e3a\u6d41\u7c7b\u578b\u3002\u5982\u679c\u9700\u8981\u4f20\u9012\u6216\u8fd4\u56deIO\u5bf9\u8c61\uff0c\u5219\u5fc5\u987b\u4f20\u9012\u6216\u8fd4\u56de\u6307\u5411\u8be5\u5bf9\u8c61\u7684\u6307\u9488\u6216\u5f15\u7528\u3002<\/p>\n<p>2\u3001IO\u6807\u51c6\u5e93\u7684\u6761\u4ef6\u72b6\u6001<\/p>\n<p>strm::iostate \u673a\u5668\u76f8\u5173\u7684\u6574\u5f62\u540d\uff0c\u7531\u5404\u4e2aiostream\u7c7b\u5b9a\u4e49\uff0c\u7528\u4e8e\u5b9a\u4e49\u6761\u4ef6\u72b6\u6001<\/p>\n<p>strm::badbit strm::iostate\u7c7b\u578b\u7684\u503c\uff0c\u7528\u4e8e\u6307\u51fa\u88ab\u7834\u574f\u7684\u6d41<\/p>\n<p>strm::failbit strm::iostate\u7c7b\u578b\u7684\u503c\uff0c\u7528\u4e8e\u6307\u51fa\u5931\u8d25\u7684IO\u64cd\u4f5c<\/p>\n<p>strm::eofbit strm::iostate\u7c7b\u578b\u7684\u503c\uff0c\u7528\u4e8e\u6307\u51fa\u6d41\u5df2\u7ecf\u5230\u8fbe\u6587\u4ef6\u7ed3\u675f\u7b26<\/p>\n<p>s.eof() \u5982\u679c\u8bbe\u7f6e\u4e86\u6d41s\u7684eofbit\u503c\uff0c\u5219\u8be5\u51fd\u6570\u8fd4\u56detrue<\/p>\n<p>s.fail() \u5982\u679c\u8bbe\u7f6e\u4e86\u6d41s\u7684failbit\u503c\uff0c\u5219\u8be5\u51fd\u6570\u8fd4\u56detrue<\/p>\n<p>s.bad() \u5982\u679c\u8bbe\u7f6e\u4e86\u6d41s\u7684badbit\u503c\uff0c\u5219\u8be5\u51fd\u6570\u8fd4\u56detrue<\/p>\n<p>s.good() \u5982\u679c\u6d41s\u5904\u4e8e\u6709\u6548\u72b6\u6001\uff0c\u5219\u8be5\u51fd\u6570\u8fd4\u56detrue<\/p>\n<p>s.clear() \u5c06\u6d41s\u4e2d\u7684\u6240\u6709\u72b6\u6001\u503c\u90fd\u91cd\u8bbe\u4e3a\u6709\u6548\u72b6\u6001<\/p>\n<p>s.clear(flag) \u5c06\u6d41s\u4e2d\u7684\u67d0\u4e2a\u6307\u5b9a\u6761\u4ef6\u72b6\u6001\u8bbe\u7f6e\u4e3a\u6709\u6548\u3002flag\u7684\u7c7b\u578b\u662fstrm::iostate<\/p>\n<p>s.setstate(flag) \u7ed9\u6d41s\u6dfb\u52a0\u6307\u5b9a\u6761\u4ef6\u3002flag\u7684\u7c7b\u578b\u662fstrm::iostate<\/p>\n<p>s.rdstate() \u8fd4\u56de\u6d41s\u7684\u5f53\u524d\u6761\u4ef6\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u4e3astrm::iostate<\/p>\n<p>badbit\u6807\u5fd7\u7740\u7cfb\u7edf\u7ea7\u7684\u6545\u969c\uff0c\u5982\u65e0\u6cd5\u6062\u590d\u7684\u8bfb\u5199\u9519\u8bef\u3002\u5982\u679c\u51fa\u73b0\u4e86\u8fd9\u7c7b\u9519\u8bef\uff0c\u5219\u8be5\u6d41\u901a\u5e38\u5c31\u4e0d\u80fd\u518d\u7ee7\u7eed\u4f7f\u7528\u4e86\u3002\u5982\u679c\u51fa\u73b0\u7684\u662f\u53ef\u6062\u590d\u7684\u9519\u8bef\uff0c\u5982\u5728\u5e0c\u671b\u83b7\u5f97\u6570\u503c\u578b\u6570\u636e\u65f6\u8f93\u5165\u4e86\u5b57\u7b26\uff0c\u6b64\u65f6\u5219\u8bbe\u7f6efailbit\u6807\u5fd7\uff0c\u8fd9\u79cd\u5bfc\u81f4\u8bbe\u7f6efailbit\u7684\u95ee\u9898\u901a\u5e38\u662f\u53ef\u4ee5\u4fee\u6b63\u7684\u3002eofbit\u662f\u5728\u9047\u5230\u6587\u4ef6\u7ed3\u675f\u7b26\u65f6\u8bbe\u7f6e\u7684\uff0c\u6b64\u65f6\u540c\u65f6\u8fd8\u8bbe\u7f6e\u4e86failbit\u3002<\/p>\n<p>3\u3001\u4e0b\u9762\u51e0\u79cd\u60c5\u51b5\u5c06\u5bfc\u81f4\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u88ab\u5237\u65b0\uff0c\u5373\u5199\u5165\u5230\u771f\u5b9e\u7684\u8f93\u51fa\u8bbe\u5907\u6216\u8005\u6587\u4ef6\uff1a<\/p>\n<p>\uff081\uff09\u7a0b\u5e8f\u6b63\u5e38\u7ed3\u675f\u3002\u4f5c\u4e3amain\u8fd4\u56de\u5de5\u4f5c\u7684\u4e00\u90e8\u5206\uff0c\u5c06\u6e05\u7a7a\u6240\u6709\u8f93\u51fa\u7f13\u51b2\u533a\u3002<\/p>\n<p>\uff082\uff09\u5728\u4e00\u4e9b\u4e0d\u786e\u5b9a\u7684\u65f6\u5019\uff0c\u7f13\u51b2\u533a\u53ef\u80fd\u5df2\u7ecf\u6ee1\u4e86\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7f13\u51b2\u533a\u5c06\u4f1a\u5728\u5199\u4e0b\u4e00\u4e2a\u503c\u4e4b\u524d\u5237\u65b0\u3002<\/p>\n<p>\uff083\uff09\u7528\u64cd\u7eb5\u7b26\u663e\u793a\u5730\u5237\u65b0\u7f13\u51b2\u533a\uff0c\u4f8b\u5982\u884c\u7ed3\u675f\u7b26endl\u3002<\/p>\n<p>\uff084\uff09\u5728\u6bcf\u6b21\u8f93\u51fa\u64cd\u4f5c\u6267\u884c\u5b8c\u540e\uff0c\u7528unitbuf\u64cd\u7eb5\u7b26\u8bbe\u7f6e\u6d41\u7684\u5185\u90e8\u72b6\u6001\uff0c\u4ece\u800c\u6e05\u7a7a\u7f13\u51b2\u533a\u3002<\/p>\n<p>\uff085\uff09\u53ef\u5c06\u8f93\u51fa\u6d41\u4e0e\u8f93\u5165\u6d41\u5173\u8054\uff08tie\uff09\u8d77\u6765\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5728\u8bfb\u8f93\u5165\u6d41\u65f6\u5c06\u5237\u65b0\u5176\u5173\u8054\u7684\u8f93\u51fa\u7f13\u51b2\u533a\u3002<\/p>\n<p>4\u3001\u9664\u4e86endl\u64cd\u7eb5\u7b26\uff0cC++\u8bed\u8a00\u8fd8\u63d0\u4f9b\u4e86\u53e6\u5916\u4e24\u4e2a\u7c7b\u4f3c\u7684\u64cd\u7eb5\u7b26\u3002\u7b2c\u4e00\u4e2a\u662f\u7ecf\u5e38\u4f7f\u7528\u7684flush\uff0c\u7528\u4e8e\u5237\u65b0\u6d41\uff0c\u4f46\u4e0d\u5728\u8f93\u51fa\u4e2d\u6dfb\u52a0\u4efb\u4f55\u5b57\u7b26\u3002\u7b2c\u4e8c\u4e2a\u5219\u662f\u6bd4\u8f83\u5c11\u7528\u7684ends\uff0c\u8fd9\u4e2a\u64cd\u7eb5\u7b26\u5728\u7f13\u51b2\u533a\u4e2d\u63d2\u5165\u7a7a\u5b57\u7b26null\uff0c\u7136\u540e\u5237\u65b0\u5b83\uff1a<\/p>\n<p>cout &lt;&lt; &#8220;hi!&#8221; &lt;&lt; flush;<\/p>\n<p>cout &lt;&lt; &#8220;hi!&#8221; &lt;&lt; ends;<\/p>\n<p>cout &lt;&lt; &#8220;hi!&#8221; &lt;&lt; endl;<\/p>\n<p>\u5982\u679c\u9700\u8981\u5237\u65b0\u6240\u6709\u8f93\u51fa\uff0c\u6700\u597d\u4f7f\u7528unitbuf\u64cd\u7eb5\u7b26\u3002\u8fd9\u4e2a\u64cd\u7eb5\u7b26\u5728\u6bcf\u6b21\u6267\u884c\u5b8c\u5199\u64cd\u4f5c\u540e\u90fd\u5237\u65b0\u6d41\uff1a<\/p>\n<p>cout &lt;&lt; unitbuf &lt;&lt; &#8220;first&#8221; &lt;&lt; &#8220;second&#8221; &lt;&lt; nounitbuf;<\/p>\n<p>\u7b49\u4ef7\u4e8e\uff1a<\/p>\n<p>cout &lt;&lt; &#8220;first&#8221; &lt;&lt; &#8220;second&#8221; &lt;&lt; flush;<\/p>\n<p>nounitbuf\u64cd\u7eb5\u7b26\u5c06\u6d41\u6062\u590d\u4e3a\u4f7f\u7528\u6b63\u5e38\u7684\u3001\u7531\u7cfb\u7edf\u7ba1\u7406\u7684\u7f13\u51b2\u533a\u5237\u65b0\u65b9\u5f0f\u3002<\/p>\n<p>5\u3001tie\u51fd\u6570\u53ef\u7528istream\u6216ostream\u5bf9\u8c61\u8c03\u7528\uff0c\u4f7f\u7528\u4e00\u4e2a\u6307\u5411ostream\u5bf9\u8c61\u7684\u6307\u9488\u5f62\u53c2\u3002\u8c03\u7528tie\u51fd\u6570\u65f6\uff0c\u5c06\u5b9e\u53c2\u6d41\u7ed1\u5728\u8c03\u7528\u8be5\u51fd\u6570\u7684\u5bf9\u8c61\u4e0a\u3002\u5982\u679c\u4e00\u4e2a\u6d41\u8c03\u7528tie\u51fd\u6570\u5c06\u5176\u672c\u8eab\u7ed1\u5728\u4f20\u9012\u7ed9tie\u7684ostream\u5b9e\u53c2\u5bf9\u8c61\u4e0a\uff0c\u5219\u8be5\u6d41\u4e0a\u7684\u4efb\u4f55IO\u64cd\u4f5c\u90fd\u4f1a\u5237\u65b0\u5b9e\u53c2\u6240\u5173\u8054\u7684\u7f13\u51b2\u533a\u3002\u4e00\u4e2aostream\u5bf9\u8c61\u6bcf\u6b21\u53ea\u80fd\u4e0e\u4e00\u4e2aistream\u5bf9\u8c61\u7ed1\u5728\u4e00\u8d77\u3002\u5982\u679c\u5728\u8c03\u7528tie\u51fd\u6570\u65f6\u4f20\u9012\u5b9e\u53c20\uff0c\u5219\u6253\u7834\u8be5\u6d41\u4e0a\u5df2\u5b58\u5728\u7684\u6346\u7ed1\u3002<\/p>\n<p>6\u3001\u7531\u4e8e\u5386\u53f2\u539f\u56e0\uff0cIO\u6807\u51c6\u5e93\u4f7f\u7528C\u98ce\u683c\u5b57\u7b26\u4e32\u800c\u4e0d\u662fC++ string\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u4f5c\u4e3a\u6587\u4ef6\u540d\u3002\u5728\u521b\u5efafstream\u5bf9\u8c61\u65f6\uff0c\u5982\u679c\u8c03\u7528open\u6216\u4f7f\u7528\u6587\u4ef6\u540d\u4f5c\u521d\u59cb\u5316\u5f0f\uff0c\u9700\u8981\u4f20\u9012\u7684\u5b9e\u53c2\u5e94\u4e3aC\u98ce\u683c\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u662f\u6807\u51c6\u5e93string\u5bf9\u8c61\u3002\u7a0b\u5e8f\u5e38\u5e38\u4ece\u6807\u51c6\u8f93\u5165\u83b7\u5f97\u6587\u4ef6\u540d\u3002\u901a\u5e38\uff0c\u6bd4\u8f83\u597d\u7684\u65b9\u6cd5\u662f\u5c06\u6587\u4ef6\u540d\u8bfb\u5165string\u5bf9\u8c61\uff0c\u800c\u4e0d\u662fC\u98ce\u683c\u5b57\u7b26\u6570\u7ec4\u3002\u5047\u8bbe\u8981\u4f7f\u7528\u7684\u6587\u4ef6\u540d\u4fdd\u5b58\u5728string\u5bf9\u8c61\u4e2d\uff0c\u5219\u53ef\u8c03\u7528c_str\u6210\u5458\u83b7\u53d6C\u98ce\u683c\u5b57\u7b26\u4e32\u3002<\/p>\n<p>7\u3001\u5982\u679c\u7a0b\u5e8f\u5458\u9700\u8981\u91cd\u7528\u6587\u4ef6\u6d41\u8bfb\u5199\u591a\u4e2a\u6587\u4ef6\uff0c\u5fc5\u987b\u5728\u8bfb\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e4b\u524d\u8c03\u7528close\u5173\u95ed\u524d\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u8c03\u7528clear\u6e05\u9664\u8be5\u6d41\u7684\u72b6\u6001\u3002<\/p>\n<p>8\u3001\u6587\u4ef6\u6a21\u5f0f<\/p>\n<p>in \u6253\u5f00\u6587\u4ef6\u505a\u8bfb\u64cd\u4f5c<\/p>\n<p>out \u6253\u5f00\u6587\u4ef6\u505a\u5199\u64cd\u4f5c<\/p>\n<p>app \u5728\u6bcf\u6b21\u5199\u4e4b\u524d\u627e\u5230\u6587\u4ef6\u5c3e<\/p>\n<p>ate \u6253\u5f00\u6587\u4ef6\u540e\u7acb\u5373\u5c06\u6587\u4ef6\u5b9a\u4f4d\u5728\u6587\u4ef6\u5c3e<\/p>\n<p>trunc \u6253\u5f00\u6587\u4ef6\u65f6\u6e05\u7a7a\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u6d41<\/p>\n<p>binary \u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u8fdb\u884cIO\u64cd\u4f5c<\/p>\n<p>9\u3001stringsteam\u63d0\u4f9b\u8f6c\u6362\u548c\/\u6216\u683c\u5f0f\u5316<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001IO\u5bf9\u8c61\u4e0d\u53ef\u590d\u5236\u6216\u8d4b\u503c\uff1a\u53ea\u6709\u652f\u6301\u590d\u5236\u7684\u5143\u7d20\u7c7b\u578b\u53ef\u4ee5\u5b58\u50a8\u5728vector\u6216\u5176\u4ed6\u5bb9\u5668\u7c7b\u578b\u91cc\uff1b\u5f62\u53c2\u6216\u8fd4\u56de\u7c7b\u578b\u4e5f\u4e0d\u80fd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-425","post","type-post","status-publish","format-standard","hentry","category-cplusplusprimer"],"_links":{"self":[{"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/posts\/425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/115.29.108.60\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=425"}],"version-history":[{"count":3,"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/posts\/425\/revisions"}],"predecessor-version":[{"id":546,"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/posts\/425\/revisions\/546"}],"wp:attachment":[{"href":"http:\/\/115.29.108.60\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/115.29.108.60\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=425"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/115.29.108.60\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}