{"id":184,"date":"2023-10-29T19:08:40","date_gmt":"2023-10-29T11:08:40","guid":{"rendered":"http:\/\/blog.minegician.cn\/?p=184"},"modified":"2023-11-01T16:11:42","modified_gmt":"2023-11-01T08:11:42","slug":"%e7%8e%b0%e4%bb%a3c%e5%ad%a6%e4%b9%a0-%e6%a0%87%e5%87%86io%e5%ba%93%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/blog.minegician.cn\/?p=184","title":{"rendered":"\u73b0\u4ee3C++\u5b66\u4e60\u2014\u2014\u6807\u51c6IO\u5e93\u7684\u4f7f\u7528"},"content":{"rendered":"<h4>IO \u5e93\u5f15\u5165<\/h4>\n<blockquote>\n<p>IO \u5e93\u57fa\u672c\u7ed3\u6784\uff08\u7f29\u8fdb\u8868\u793a\u7ee7\u627f\u5c42\u7ea7\uff09\uff1a<\/p>\n<p><code>ios(\u4e00\u822c\u7528\u4e0d\u5230)<\/code> <\/p>\n<p>\u200b \u2192 <code>istream \/ ostream(\u5c5e\u4e8e\u5934\u6587\u4ef6&lt;iostream&gt;)<\/code> <\/p>\n<p>\u200b     \u2192 <code>ifstream \/ ofstream(\u5c5e\u4e8e\u5934\u6587\u4ef6&lt;fstream&gt;)<\/code><\/p>\n<p>\u200b     \u2192 <code>istringstream \/ ostringstream(\u5c5e\u4e8e\u5934\u6587\u4ef6&lt;sstream&gt;)<\/code><\/p>\n<ol>\n<li>\n<p>\u4ee5\u4e0a\u7684\u8f93\u5165\u8f93\u51fa\u90fd\u662f\u5bf9\u5185\u5b58\u7684\u8f93\u5165\u8f93\u51fa<\/p>\n<\/li>\n<li>\n<p>\u7ee7\u627f\u7684\u7c7b\u4e5f\u91cd\u8f7d\u4e86 <code>&lt;&lt;<\/code> <code>&gt;&gt;<\/code> \u8fd0\u7b97\u7b26\uff0c\u4e3a\u6587\u4ef6 \/ \u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u540c\u6837\u7684\u8f93\u5165\u8f93\u51fa\u529f\u80fd<\/p>\n<\/li>\n<li>\n<p>\u6807\u51c6\u5e93\u4e2d\u6a21\u677f\u7c7b\u7684\u7ee7\u627f\u5173\u7cfb\u5982\u4e0b\uff08\u53bb\u6389 <code>basic<\/code> \u524d\u7f00\u7684\u5c31\u662f <code>&lt;char&gt;<\/code> \u7684\u7279\u5316\u7248\uff0c\u4e00\u822c\u4e5f\u53ea\u8ba8\u8bba\u4ed6\u4eec\uff09\uff1a<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/mgc-1311747586.cos.ap-shanghai.myqcloud.com\/notes\/std-io-complete-inheritance.svg'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/mgc-1311747586.cos.ap-shanghai.myqcloud.com\/notes\/std-io-complete-inheritance.svg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"std-io-complete-inheritance.svg\" \/><\/div><\/p>\n<\/li>\n<\/ol>\n<\/blockquote>\n<p>IO \u5e93\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<ol>\n<li>\n<p>\u5173\u4e8e IO \u5bf9\u8c61\u7684\u6784\u9020\u548c\u8d4b\u503c\u7b49\u51fd\u6570<\/p>\n<blockquote>\n<ul>\n<li>\n<p><code>delete<\/code> \u4e86\u62f7\u8d1d\u6784\u9020\u548c <code>operator=<\/code>\uff1a\u62f7\u8d1d\u6d41\u5bf9\u8c61\u6570\u636e\u4f1a\u5bfc\u81f4\u5185\u5b58\u6df7\u4e71\u3002\u5e94\u8be5\u4f7f\u7528\u5f15\u7528\u6307\u5411\u5185\u5b58<\/p>\n<p>\u56e0\u6b64\uff0c\u91cd\u8f7d\u6d41\u8fd0\u7b97\u7b26\u7684\u8fd4\u56de\u503c\u90fd\u662f\u5f15\u7528<\/p>\n<\/li>\n<li>\n<p>\u53f3\u503c\u62f7\u8d1d\u662f <code>protected<\/code>\uff0c\u7528\u4e8e\u7ee7\u627f\u548c <code>extern<\/code> \u5b9a\u4e49\u5168\u5c40\u5bf9\u8c61\uff08\u6bd4\u5982 <code>cin<\/code>\uff09\uff0c\u800c\u4e0d\u80fd\u624b\u52a8\u5b9e\u4f8b\u5316<\/p>\n<\/li>\n<\/ul>\n<\/blockquote>\n<\/li>\n<li>\n<p>IO \u5bf9\u8c61\u7684\u72b6\u6001<\/p>\n<blockquote>\n<p>IO \u5e93\u5b9a\u4e49\u4e86 <code>iostate<\/code> \u8868\u793a IO \u5bf9\u8c61\u7684\u72b6\u6001\uff0c\u5728 VS \u4e2d\u662f <code>int<\/code> \u5f62\u5f0f\uff0c\u901a\u8fc7\u5c06\u4e8c\u8fdb\u5236\u4f4d\u7f6e 0\/1 \u8bbe\u7f6e\u72b6\u6001<\/p>\n<ul>\n<li>\n<p>IO \u5bf9\u8c61\u72b6\u6001\u6709 4 \u79cd\uff1a<\/p>\n<ol>\n<li>\n<p>goodbit\uff0c\u6ca1\u6709\u9519\u8bef<\/p>\n<\/li>\n<li>\n<p>failbit\uff0c\u9519\u8bef\uff0c\u5404\u79cd\u9519\u8bef<\/p>\n<p>eofbit\uff0c\u6587\u4ef6\u7ed3\u675f\uff0c\u540c\u65f6\u8bbe\u7f6e failbit<\/p>\n<p>badbit\uff0c\u7cfb\u7edf\u7ea7\u9519\u8bef\uff0c\u540c\u65f6\u8bbe\u7f6e failbit<\/p>\n<\/li>\n<\/ol>\n<ul>\n<li>\u4e5f\u5c31\u662f\u8bf4\u53ea\u8981\u51fa\u73b0\u4e86\u9519\u8bef\uff0c\u90fd\u4f1a\u8bbe\u7f6e failbit\uff0c<code>cin.good()<\/code> \u7b49\u6548 <code>!cin.fail()<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u540c\u65f6\u6807\u51c6\u5e93\u5bf9\u8c61\u63d0\u4f9b\u4e86\u4e00\u4e9b\u72b6\u6001\u76f8\u5173\u7684\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-c++\">\/\/ \u8fd4\u56de bool\ncin.good();\ncin.fail();\ncin.eof();\ncin.bad();<\/code><\/pre>\n<pre><code class=\"language-c++\">\/\/ \u5c11\u6570\u8c03\u8bd5\u7528\uff0c\u53ef\u4ee5\u5ffd\u7565\nint state = rdstate();   \/\/ \u8fd4\u56de\u5f53\u524dIO\u72b6\u6001(\u76f4\u63a5\u4ee5int\u5f62\u5f0f)\ncin.setstate(flag);\/\/ \u8bbe\u7f6estate<\/code><\/pre>\n<pre><code class=\"language-c++\">\/\/ \u91cd\u7f6e\u72b6\u6001\ncin.clear(); \/\/ \u91cd\u7f6e\u4e3agood\ncin.clear(flag); \/\/ \u91cd\u7f6e\u7279\u5b9a\u4f4d\n\n\/\/ \u63d0\u53d6\u8f93\u5165\u5e76\u4e22\u5f03\ncin.ignore(streamsize, delimeter);   \/\/ ignore\u7684\u957f\u5ea6\uff0c\u5230\u54ea\u4e2a\u5b57\u7b26\u5904\u505c\u6b62<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/blockquote>\n<\/li>\n<\/ol>\n<ul>\n<li>\n<p>\u4e00\u6bb5\u4f01\u4e1a\u7ea7\u8f93\u5165demo\uff1a<\/p>\n<pre><code class=\"language-c++\">#include <iostream>\n\nint main(){\n  int i = 10;\n  while(std::cin >> i, !std::cin.eof()){    \/\/ \u9017\u53f7\u7528\u4e8e\u524d\u9762\u7684\u5f0f\u5b50\u53ea\u6267\u884c\u4e0d\u8fd4\u56de\u503c\u7684\u60c5\u51b5\n      if(std::cin.bad()){       \/\/ \u7cfb\u7edf\u7ea7\u9519\u8bef\u629b\u51fa\u5f02\u5e38\n          throw std::runtime_error(\"cin is corrupted\");\n      }\n      if(std::cin.fail()){  \/\/ \u5269\u4e0b\u7684\u53ea\u6709\u666e\u901a\u9519\u8bef\n          std::cin.clear();\n          std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\\n');   \/\/ \u5ffd\u7565\u5f53\u524d\u6574\u884c\n          std::cout << \"format error, please try again\" << std::endl;\n          continue;\n      }\n      std::cout << \"i = \" << i << std::endl;\n  }\n  std::cout << \"process complete\" << std::endl; \n}<\/code><\/pre>\n<blockquote>\n<p><code>basic_istream<\/code>\uff0c<code>basic_ostream<\/code> \u91cd\u8f7d\u4e86 <code>operator bool()<\/code>\uff0c\u7528\u4e8e\u5b9e\u73b0\u4f5c\u4e3a\u5224\u65ad\u6761\u4ef6\u7684\u8fd4\u56de\u503c\u903b\u8f91<\/p>\n<p>\u4f46\u662f <code>operator bool()<\/code> \u4e00\u4e2a\u51fd\u6570\u4e0d\u80fd\u533a\u5206\u4e0d\u540c\u7684\u9519\u8bef\uff0c\u6240\u4ee5\u4e0a\u8ff0\u4ee3\u7801\u4f7f\u7528\u4e86\u6210\u5458\u51fd\u6570\u6765\u5224\u65ad<\/p>\n<\/blockquote>\n<\/li>\n<\/ul>\n<h4>iostream \u7684\u8865\u5145<\/h4>\n<ul>\n<li>\n<p>IO \u5e93\u63d0\u4f9b\u7684\u5176\u4ed6\u8f93\u5165\uff1a<\/p>\n<p><code>std::getline(istream, string, [delimeter])<\/code> \u2192 <code>basic_istream<\/code><\/p>\n<pre><code class=\"language-c++\">std::string str;\n\/\/ \u5c5e\u4e8e\u5934\u6587\u4ef6<string>\nstd::getline(std::cin, str);<\/code><\/pre>\n<p><code>std::cin.get(char)<\/code> \u2192 <code>basic_istream<\/code><\/p>\n<pre><code class=\"language-c++\">char c;\nstd::cin.get(c);<\/code><\/pre>\n<blockquote>\n<ul>\n<li>\n<p><code>std::getline( )<\/code> \u548c <code>cin.getline( )<\/code> \u7684\u533a\u522b\uff1a<\/p>\n<p>\u524d\u8005\u662f\u7b26\u53f7\u622a\u53d6\u672b\u5c3e\u518d\u7ed9 <code>string<\/code>\uff0c\u540e\u8005\u662f\u624b\u52a8\u6307\u5b9a\u957f\u5ea6\u518d\u7ed9 <code>char*<\/code>\uff0c\u663e\u7136\u524d\u8005\u76f8\u5bf9\u597d\u7528\u4e9b<\/p>\n<\/li>\n<li>\n<p>\u5b9e\u9645\u5de5\u7a0b\u4e2d\u9700\u8981\u8fdb\u884c\u9519\u8bef\u5904\u7406<\/p>\n<\/li>\n<\/ul>\n<\/blockquote>\n<\/li>\n<\/ul>\n<h4>\u6587\u4ef6\u6d41 fstream<\/h4>\n<blockquote>\n<p>\u76f8\u6bd4 <code>&lt;iostream&gt;<\/code>\uff0c<code>&lt;fstream&gt;<\/code> \u6ca1\u6709\u63d0\u4f9b\u9ed8\u8ba4\u7684\u6d41\u5bf9\u8c61<\/p>\n<p>\u4f46\u76f8\u5bf9\u7684\uff0c<code>fstream<\/code>\uff0c<code>ifstream<\/code>\uff0c<code>ofstream<\/code> \u652f\u6301\u4e86 public \u6784\u9020\uff08\u5305\u62ec\u53f3\u503c\u62f7\u8d1d\uff09\uff0c\u53ef\u4ee5\u624b\u52a8\u6784\u9020<\/p>\n<ul>\n<li>\u8fd9\u4e09\u4e2a\u7c7b\u6ca1\u6709\u7ee7\u627f\u5173\u7cfb\uff0c\u6240\u4ee5\u5728\u6587\u4ef6\u529f\u80fd\u4e0a\u5404\u81ea\u72ec\u7acb\uff0c<code>fstream<\/code> \u6b63\u597d\u540c\u65f6\u5b9e\u73b0\u4e86\u8f93\u5165\u548c\u8f93\u51fa<\/li>\n<\/ul>\n<\/blockquote>\n<pre><code class=\"language-c++\">\/\/ \u7b2c\u4e8c\u4e2a\u53c2\u6570\u53ef\u4ee5\u7701\u7565\nstd::fstream fs(&quot;test.txt&quot;, std::ios::in | std::ios::out);\n\/\/ \u6784\u9020\u548c\u6253\u5f00\u6587\u4ef6\u53ef\u4ee5\u5206\u5f00\uff0c\u4f46\u4e00\u5b9a\u8981\u5173\u95ed\nstd::ifstream ifs;\nifs.open(&quot;test.txt&quot;);\nifs.close();\n\/\/ \u6a21\u5f0f\u6709\u5f88\u591a\u79cd\nstd::ofstream ofs(&quot;test.txt&quot;, std::ios::app);<\/code><\/pre>\n<ol>\n<li>\n<p>\u9ed8\u8ba4\u7684\u6a21\u5f0f\u90fd\u662f <code>std::ios::in<\/code> \u6216 <code>std::ios::out<\/code><\/p>\n<\/li>\n<li>\n<p>\u6587\u4ef6\u5bf9\u8c61\u7684\u5185\u5b58\u4e0d\u5728\u6808\u6216\u5806\uff0c\u800c\u662f\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\uff0c\u662f\u667a\u80fd\u6307\u9488\u4e5f\u7ba1\u4e0d\u5230\u7684\u5730\u65b9\uff0c<code>open<\/code> \u4e4b\u540e\u53ea\u80fd\u624b\u52a8 <code>close<\/code><\/p>\n<\/li>\n<li>\n<p>\u6587\u4ef6\u8bfb\u5199\u6a21\u5f0f\uff08\u53ef\u4ee5\u7c7b\u6bd4\u6307\u9488\uff09\uff1a<\/p>\n<p><code>app<\/code> \u5199\u7684\u65f6\u5019\u8df3\u5230\u672b\u5c3e\uff1b<code>in<\/code> \u76f4\u63a5\u8bfb\uff1b<code>out<\/code> \u76f4\u63a5\u5199\uff1b<\/p>\n<p><code>trunc<\/code> \u6253\u5f00\u65f6\u6e05\u7a7a\u5185\u5bb9\uff1b<code>ate<\/code> \u6253\u5f00\u76f4\u63a5\u8df3\u5230\u672b\u5c3e\uff1b<\/p>\n<p><code>binary<\/code> \u4e8c\u8fdb\u5236\u6a21\u5f0f\uff1b\uff08<code>noreplace<\/code> \u72ec\u5360\u6a21\u5f0f\uff0cC++23\uff09<\/p>\n<\/li>\n<\/ol>\n<ul>\n<li>\n<p>\u6a21\u5f0f\u6bd4\u8f83\u7384\u5b66\uff0c\u4ee5\u4e0b\u662f\u76ee\u524d\u603b\u7ed3\u7684\uff1a<\/p>\n<blockquote>\n<ul>\n<li>\n<p>\u6ca1\u6709\u5728\u8bfb\u6587\u4ef6\u65f6\uff0c\u624d\u4f1a\u5728\u6587\u4ef6\u4e0d\u5b58\u5728\u65f6\u65b0\u5efa\u6587\u4ef6<\/p>\n<\/li>\n<li>\n<p>\u5c1a\u65e0\u6cd5\u786e\u8ba4 <code>out<\/code> \u548c <code>trunc<\/code> \u7684\u533a\u522b\uff0c\u4f46\u6709\u4ee5\u4e0b\u89c4\u5219\uff1a<\/p>\n<ol>\n<li>\n<p><code>fstream<\/code> \u4e0d\u80fd\u76f4\u63a5\u7528 <code>trunc<\/code>\uff0c<code>ostream<\/code> \u4e2d <code>trunc<\/code> \u4e0e <code>in<\/code> \u5171\u5b58\u4ecd\u4f1a\u6e05\u7a7a\u6587\u4ef6<\/p>\n<\/li>\n<li>\n<p>\u53ea\u6709 <code>out<\/code> \u65f6\u4f1a\u6e05\u7a7a\u91cd\u5199\uff0c\u5f53 <code>in<\/code> \u548c <code>out<\/code> \u5171\u5b58\u65f6\u5219\u4e0d\u4f1a<\/p>\n<\/li>\n<li>\n<p><code>trunc<\/code> \u5728 <code>fstream<\/code> \u4e2d\u8981\u914d\u5408 <code>out<\/code>\uff0c\u5728 <code>ofstream<\/code> \u4e2d\u4e0d\u4ec5\u4e0d\u9700\u8981 <code>out<\/code> \u8fd8\u53ef\u4ee5\u65e0\u89c6 <code>in<\/code><\/p>\n<\/li>\n<\/ol>\n<ul>\n<li>\u6240\u4ee5\u4e0d\u60f3\u6e05\u7a7a\u7684\u65f6\u5019\u4e00\u5f8b\u52a0\u4e0a <code>in<\/code>\uff0c\u60f3\u6e05\u7a7a\u7684\u8bdd\u4f7f\u7528 <code>ostream<\/code>+<code>trunc<\/code>\uff0c\u8ffd\u52a0\u5219\u624b\u52a8\u8bbe\u4e3a <code>app<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>ate<\/code> \u548c <code>binary<\/code> \u662f\u8f85\u52a9\u7528\u7684\uff0c\u4e0d\u4f1a\u4e0e\u5176\u4ed6\u6a21\u5f0f\u4ea7\u751f\u5947\u5999\u7684\u53cd\u5e94<\/p>\n<\/li>\n<\/ul>\n<\/blockquote>\n<\/li>\n<li>\n<p>\u4e00\u6bb5\u4f01\u4e1a\u7ea7\u8bfb\u6587\u4ef6demo\uff1a<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"language-c++\">#include &lt;fstream&gt;\n#include &lt;iostream&gt;\n#include &lt;string&gt;\n\nint main() {\n    std::string fileName;\n    std::string fileLine;\n    while(std::cin &gt;&gt; fileName, !std::cin.eof()) {\n        if(std::cin.bad()) {\n            throw std::runtime_error(&quot;cin corrupted&quot;);\n        }\n        std::ifstream ifs(fileName);\n        if(ifs.is_open()) {\n            while(std::getline(ifs, fileLine)) {\n                std::cout &lt;&lt; fileLine &lt;&lt; std::endl;\n            }\n            if(ifs.bad()) {\n                throw std::runtime_error(&quot;file stream corrupted&quot;);\n            }\n        }else {\n            std::cin.clear();\n            std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), &#039;\\n&#039;);\n            std::cout &lt;&lt; &quot;file not exist, please try again&quot; &lt;&lt; std::endl;\n            continue;\n        }\n    }\n    std::cout &lt;&lt; &quot;process complete&quot; &lt;&lt; std::endl;\n}<\/code><\/pre>\n<h4>\u5b57\u7b26\u4e32\u6d41 sstream<\/h4>\n<blockquote>\n<p>\u548c <code>&lt;fstream&gt;<\/code> \u540c\u4e3a <code>&lt;iostream&gt;<\/code> \u4e0b\u7c7b\u7684\u6d3e\u751f\uff0c\u7ee7\u627f\u5173\u7cfb\u4e0e <code>fstream<\/code> \u7cfb\u5217\u5b8c\u5168\u4e00\u81f4\uff0c\u9700\u8981\u624b\u52a8\u521d\u59cb\u5316<\/p>\n<p>\u6b64\u5916\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u6d41\u4e24\u7aef\u90fd\u662f\u5185\u5b58\u4e2d\u7684\u5bf9\u8c61\uff0c\u6240\u4ee5\u6709\u5f88\u591a\u72ec\u6709\u7684\u7279\u6027<\/p>\n<\/blockquote>\n<pre><code class=\"language-c++\">\/\/ \u5185\u7f6e\u4e00\u4e2astring\uff0c\u9ed8\u8ba4\u6784\u9020\u4e5f\u4f1a\u4fdd\u5b58\u4e00\u4e2a\u7a7astring\nstd::stringstream ss;\nstd::cout &lt;&lt; ss.str() &lt;&lt; std::endl; \/\/ \u65e0\u53c2 str() \u8fd4\u56de\u5185\u7f6e\u4e32\n\/\/ \u666e\u901a\u6784\u9020\uff0c\u62f7\u8d1d\u4e00\u4e2a\u4e32\u8fdb\u53bb\nstd::ostringstream oss(&quot;test&quot;);\n\/\/ \u5e26\u53c2 str(string) \u62f7\u8d1d\u4e32\nstd::istringstream iss;\niss.str(&quot;test&quot;);<\/code><\/pre>\n<h5>\u7279\u6027\u6f14\u793a<\/h5>\n<blockquote>\n<p>\u4f53\u73b0\u4e00\u4e9b iostream \u5904\u7406\u5b57\u7b26\u4e32\u7684\u7279\u6027\uff08\u5f3a\u5927\u7684 <code>&lt;&lt;<\/code> <code>&gt;&gt;<\/code>\uff09<\/p>\n<\/blockquote>\n<ul>\n<li>\u5229\u7528\u7c7b\u63d0\u4f9b\u7684\u64cd\u4f5c\u5c06\u4e32\u53d8\u6210\u6570<\/li>\n<\/ul>\n<pre><code class=\"language-c++\">#include &lt;iostream&gt;\n#include &lt;sstream&gt;\n\nint main() {\n    std::string str(&quot;12&quot;);\n    std::stringstream ss(str);\n\n    int i = 0;\n    ss &gt;&gt; i;\n    if(ss.bad()) {\n        throw std::runtime_error(&quot;ss corrupted&quot;);\n    }else if(ss.fail()) {\n        ss.clear();\n        ss.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), &#039;\\n&#039;);\n        std::cout &lt;&lt; &quot;string format error&quot; &lt;&lt; std::endl;\n    }else {\n        std::cout &lt;&lt; i &lt;&lt; std::endl;    \/\/ 12\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n<ul>\n<li>\u5c06\u6570\u8f6c\u4e3a\u4e32<\/li>\n<\/ul>\n<pre><code class=\"language-c++\">#include &lt;iostream&gt;\n#include &lt;sstream&gt;\n\nint main() {\n    int test = 100;\n    std::stringstream ss;\n    ss &lt;&lt; test &lt;&lt; std::endl;\n    if(ss.bad()) {\n        throw std::runtime_error(&quot;ss corrupted&quot;);\n    }else {\n        std::cout &lt;&lt; ss.str() &lt;&lt; std::endl;\n    }\n}<\/code><\/pre>\n<ul>\n<li>\u4e32\u7684\u5207\u5206\uff08\u6a21\u4eff <code>cin<\/code>\uff09<\/li>\n<\/ul>\n<pre><code class=\"language-c++\">#include &lt;iostream&gt;\n#include &lt;sstream&gt;\n\nint main() {\n    std::string src_str(&quot;hello world mgc xcr&quot;);\n    std::string dest_str;\n    std::stringstream ss(src_str);\n    while (ss &gt;&gt; dest_str) {\n        std::cout &lt;&lt; dest_str &lt;&lt; std::endl;\n    }\n    if(ss.bad()) {\n        throw std::runtime_error(&quot;ss ccorrupted&quot;);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>IO \u5e93\u5f15\u5165 IO \u5e93\u57fa\u672c\u7ed3\u6784\uff08\u7f29\u8fdb\u8868\u793a\u7ee7\u627f\u5c42\u7ea7\uff09\uff1a ios(\u4e00\u822c\u7528\u4e0d\u5230) \u200b \u2192 istream \/ ost [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":185,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-184","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/blog.minegician.cn\/index.php?rest_route=\/wp\/v2\/posts\/184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.minegician.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.minegician.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.minegician.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.minegician.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=184"}],"version-history":[{"count":1,"href":"http:\/\/blog.minegician.cn\/index.php?rest_route=\/wp\/v2\/posts\/184\/revisions"}],"predecessor-version":[{"id":186,"href":"http:\/\/blog.minegician.cn\/index.php?rest_route=\/wp\/v2\/posts\/184\/revisions\/186"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/blog.minegician.cn\/index.php?rest_route=\/wp\/v2\/media\/185"}],"wp:attachment":[{"href":"http:\/\/blog.minegician.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.minegician.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=184"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.minegician.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}