{"id":377,"date":"2020-11-03T12:33:33","date_gmt":"2020-11-03T16:33:33","guid":{"rendered":"http:\/\/www.prglab.com\/blog\/?page_id=377"},"modified":"2020-11-03T12:33:33","modified_gmt":"2020-11-03T16:33:33","slug":"file-input-output","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/cplusplus-tutorial\/standard-library\/file-input-output","title":{"rendered":"\u6587\u4ef6\u7684\u8f93\u5165\u8f93\u51fa"},"content":{"rendered":"\n<h2>6.1 \u6587\u4ef6\u7684\u8f93\u5165\u8f93\u51fa (Input\/Output with files)<\/h2>\n\n\n\n<p>C++ \u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u7c7b\u652f\u6301\u6587\u4ef6\u7684\u8f93\u5165\u8f93\u51fa\uff1a<\/p>\n\n\n\n<ul><li>ofstream: \u5199\u64cd\u4f5c\uff08\u8f93\u51fa\uff09\u7684\u6587\u4ef6\u7c7b (\u7531ostream\u5f15\u7533\u800c\u6765)<\/li><li>ifstream: \u8bfb\u64cd\u4f5c\uff08\u8f93\u5165\uff09\u7684\u6587\u4ef6\u7c7b(\u7531istream\u5f15\u7533\u800c\u6765)<\/li><li>fstream: \u53ef\u540c\u65f6\u8bfb\u5199\u64cd\u4f5c\u7684\u6587\u4ef6\u7c7b (\u7531iostream\u5f15\u7533\u800c\u6765)<\/li><\/ul>\n\n\n\n<h3>\u6253\u5f00\u6587\u4ef6(Open a file)<\/h3>\n\n\n\n<p>\u5bf9\u8fd9\u4e9b\u7c7b\u7684\u4e00\u4e2a\u5bf9\u8c61\u6240\u505a\u7684\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u901a\u5e38\u5c31\u662f\u5c06\u5b83\u548c\u4e00\u4e2a\u771f\u6b63\u7684\u6587\u4ef6\u8054\u7cfb\u8d77\u6765\uff0c\u4e5f\u5c31\u662f\u8bf4\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u3002\u88ab\u6253\u5f00\u7684\u6587\u4ef6\u5728\u7a0b\u5e8f\u4e2d\u7531\u4e00\u4e2a\u6d41\u5bf9\u8c61(stream object)\u6765\u8868\u793a (\u8fd9\u4e9b\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b) \uff0c\u800c\u5bf9\u8fd9\u4e2a\u6d41\u5bf9\u8c61\u6240\u505a\u7684\u4efb\u4f55\u8f93\u5165\u8f93\u51fa\u64cd\u4f5c\u5b9e\u9645\u5c31\u662f\u5bf9\u8be5\u6587\u4ef6\u6240\u505a\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u8981\u901a\u8fc7\u4e00\u4e2a\u6d41\u5bf9\u8c61\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u6211\u4eec\u4f7f\u7528\u5b83\u7684\u6210\u5458\u51fd\u6570open()\uff1a<code>void open (const char * filename, openmode mode);<\/code><\/p>\n\n\n\n<p>\u8fd9\u91ccfilename \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4ee3\u8868\u8981\u6253\u5f00\u7684\u6587\u4ef6\u540d\uff0cmode \u662f\u4ee5\u4e0b\u6807\u5fd7\u7b26\u7684\u4e00\u4e2a\u7ec4\u5408\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>ios::in<\/td><td>\u4e3a\u8f93\u5165(\u8bfb)\u800c\u6253\u5f00\u6587\u4ef6<\/td><\/tr><tr><td>ios::out<\/td><td>\u4e3a\u8f93\u51fa(\u5199)\u800c\u6253\u5f00\u6587\u4ef6<\/td><\/tr><tr><td>ios::ate<\/td><td>\u521d\u59cb\u4f4d\u7f6e\uff1a\u6587\u4ef6\u5c3e<\/td><\/tr><tr><td>ios::app<\/td><td>\u6240\u6709\u8f93\u51fa\u9644\u52a0\u5728\u6587\u4ef6\u672b\u5c3e<\/td><\/tr><tr><td>ios::trunc<\/td><td>\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\u5219\u5148\u5220\u9664\u8be5\u6587\u4ef6<\/td><\/tr><tr><td>ios::binary<\/td><td>\u4e8c\u8fdb\u5236\u65b9\u5f0f<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u4e9b\u6807\u8bc6\u7b26\u53ef\u4ee5\u88ab\u7ec4\u5408\u4f7f\u7528\uff0c\u4e2d\u95f4\u4ee5\u201d\u6216\u201d\u64cd\u4f5c\u7b26(|)\u95f4\u9694\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u4ee5\u4e8c\u8fdb\u5236\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6&#8221;example.bin&#8221; \u6765\u5199\u5165\u4e00\u4e9b\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8c03\u7528\u6210\u5458\u51fd\u6570open\uff08\uff09\u6765\u5b9e\u73b0\uff1a<code>ofstream file;<br>file.open (\"example.bin\", ios::out | ios::app | ios::binary);<\/code><\/p>\n\n\n\n<p>ofstream, ifstream \u548c fstream\u6240\u6709\u8fd9\u4e9b\u7c7b\u7684\u6210\u5458\u51fd\u6570open \u90fd\u5305\u542b\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u6253\u5f00\u6587\u4ef6\u7684\u65b9\u5f0f\uff0c\u8fd9\u4e09\u4e2a\u7c7b\u7684\u9ed8\u8ba4\u65b9\u5f0f\u5404\u4e0d\u76f8\u540c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>\u7c7b<\/th><th>\u53c2\u6570\u7684\u9ed8\u8ba4\u65b9\u5f0f<\/th><\/tr><tr><td>ofstream<\/td><td>ios::out | ios::trunc<\/td><\/tr><tr><td>ifstream<\/td><td>ios::in<\/td><\/tr><tr><td>fstream<\/td><td>ios::in | ios::out<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53ea\u6709\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u6ca1\u6709\u58f0\u660e\u65b9\u5f0f\u53c2\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u9ed8\u8ba4\u503c\u624d\u4f1a\u88ab\u91c7\u7528\u3002\u5982\u679c\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u58f0\u660e\u4e86\u4efb\u4f55\u53c2\u6570\uff0c\u9ed8\u8ba4\u503c\u5c06\u88ab\u5b8c\u5168\u6539\u5199\uff0c\u800c\u4e0d\u4f1a\u4e0e\u8c03\u7528\u53c2\u6570\u7ec4\u5408\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e\u5bf9\u7c7bofstream, ifstream \u548c fstream \u7684\u5bf9\u8c61\u6240\u8fdb\u884c\u7684\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u901a\u5e38\u90fd\u662f\u6253\u5f00\u6587\u4ef6\uff0c\u8fd9\u4e9b\u7c7b\u90fd\u6709\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528open \u51fd\u6570\uff0c\u5e76\u62e5\u6709\u540c\u6837\u7684\u53c2\u6570\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8fdb\u884c\u4e0e\u4e0a\u9762\u540c\u6837\u7684\u5b9a\u4e49\u5bf9\u8c61\u548c\u6253\u5f00\u6587\u4ef6\u7684\u64cd\u4f5c\uff1a<code>ofstream file (\"example.bin\", ios::out | ios::app | ios::binary);<\/code><\/p>\n\n\n\n<p>\u4e24\u79cd\u6253\u5f00\u6587\u4ef6\u7684\u65b9\u5f0f\u90fd\u662f\u6b63\u786e\u7684\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u6210\u5458\u51fd\u6570is_open()\u6765\u68c0\u67e5\u4e00\u4e2a\u6587\u4ef6\u662f\u5426\u5df2\u7ecf\u88ab\u987a\u5229\u7684\u6253\u5f00\u4e86\uff1a<code>bool is_open();<\/code><\/p>\n\n\n\n<p>\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14(bool)\u503c\uff0c\u4e3a\u771f\uff08true\uff09\u4ee3\u8868\u6587\u4ef6\u5df2\u7ecf\u88ab\u987a\u5229\u6253\u5f00\uff0c\u5047( false )\u5219\u76f8\u53cd\u3002<\/p>\n\n\n\n<h3>\u5173\u95ed\u6587\u4ef6(Closing a file)<\/h3>\n\n\n\n<p>\u5f53\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u5b8c\u6210\u4e4b\u540e\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u6587\u4ef6\u5173\u95ed\u4ee5\u4f7f\u6587\u4ef6\u91cd\u65b0\u53d8\u4e3a\u53ef\u8bbf\u95ee\u7684\u3002\u5173\u95ed\u6587\u4ef6\u9700\u8981\u8c03\u7528\u6210\u5458\u51fd\u6570close()\uff0c\u5b83\u8d1f\u8d23\u5c06\u7f13\u5b58\u4e2d\u7684\u6570\u636e\u6392\u653e\u51fa\u6765\u5e76\u5173\u95ed\u6587\u4ef6\u3002\u5b83\u7684\u683c\u5f0f\u5f88\u7b80\u5355\uff1a<code>void close ();<\/code><\/p>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u6570\u4e00\u65e6\u88ab\u8c03\u7528\uff0c\u539f\u5148\u7684\u6d41\u5bf9\u8c61(stream object)\u5c31\u53ef\u4ee5\u88ab\u7528\u6765\u6253\u5f00\u5176\u5b83\u7684\u6587\u4ef6\u4e86\uff0c\u8fd9\u4e2a\u6587\u4ef6\u4e5f\u5c31\u53ef\u4ee5\u91cd\u65b0\u88ab\u5176\u5b83\u7684\u8fdb\u7a0b(process)\u6240\u6709\u8bbf\u95ee\u4e86\u3002<\/p>\n\n\n\n<p>\u4e3a\u9632\u6b62\u6d41\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\u8fd8\u8054\u7cfb\u7740\u6253\u5f00\u7684\u6587\u4ef6\uff0c\u6790\u6784\u51fd\u6570(destructor)\u5c06\u4f1a\u81ea\u52a8\u8c03\u7528\u5173\u95ed\u51fd\u6570close\u3002<\/p>\n\n\n\n<h3>\u6587\u672c\u6587\u4ef6(Text mode files)<\/h3>\n\n\n\n<p>\u7c7bofstream, ifstream \u548cfstream \u662f\u5206\u522b\u4eceostream, istream \u548ciostream \u4e2d\u5f15\u7533\u800c\u6765\u7684\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48 fstream \u7684\u5bf9\u8c61\u53ef\u4ee5\u4f7f\u7528\u5176\u7236\u7c7b\u7684\u6210\u5458\u6765\u8bbf\u95ee\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u8fd9\u4e9b\u7c7b\u4e0e\u540c\u63a7\u5236\u53f0(console)\u4ea4\u4e92\u540c\u6837\u7684\u6210\u5458\u51fd\u6570(cin \u548c cout)\u6765\u8fdb\u884c\u8f93\u5165\u8f93\u51fa\u3002\u5982\u4e0b\u9762\u7684\u4f8b\u9898\u6240\u793a\uff0c\u6211\u4eec\u4f7f\u7528\u91cd\u8f7d\u7684\u63d2\u5165\u64cd\u4f5c\u7b26&lt;&lt;\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ writing on a text file #include &lt;fiostream.h&gt; int main () { ofstream examplefile (&#8220;example.txt&#8221;); if (examplefile.is_open()) { examplefile &lt;&lt; &#8220;This is a line.\\n&#8221;; examplefile &lt;&lt; &#8220;This is another line.\\n&#8221;; examplefile.close(); } return 0; }<\/td><td>file example.txt<br>This is a line.<br>This is another line.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4ece\u6587\u4ef6\u4e2d\u8bfb\u5165\u6570\u636e\u4e5f\u53ef\u4ee5\u7528\u4e0e cin\u7684\u4f7f\u7528\u540c\u6837\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ reading a text file #include &lt;iostream.h&gt; #include &lt;fstream.h&gt; #include &lt;stdlib.h&gt; int main () { char buffer[256]; ifstream examplefile (&#8220;example.txt&#8221;); if (! examplefile.is_open()) { cout &lt;&lt; &#8220;Error opening file&#8221;; exit (1); } while (! examplefile.eof() ) { examplefile.getline (buffer,100); cout &lt;&lt; buffer &lt;&lt; endl; } return 0; }<\/td><td>This is a line.<br>This is another line.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u8bfb\u5165\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u7136\u540e\u5c06\u5b83\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002\u6ce8\u610f\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u65b0\u7684\u6210\u5458\u51fd\u6570\u53eb\u505aeof \uff0c\u5b83\u662fifstream \u4ece\u7c7b ios \u4e2d\u7ee7\u627f\u8fc7\u6765\u7684\uff0c\u5f53\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e\u65f6\u8fd4\u56detrue \u3002<\/p>\n\n\n\n<h3>\u72b6\u6001\u6807\u5fd7\u7b26\u7684\u9a8c\u8bc1(Verification of state flags)<\/h3>\n\n\n\n<p>\u9664\u4e86eof()\u4ee5\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u9a8c\u8bc1\u6d41\u7684\u72b6\u6001\u7684\u6210\u5458\u51fd\u6570\uff08\u6240\u6709\u90fd\u8fd4\u56debool\u578b\u8fd4\u56de\u503c\uff09\uff1a<\/p>\n\n\n\n<ul><li><strong>bad()<\/strong>\u5982\u679c\u5728\u8bfb\u5199\u8fc7\u7a0b\u4e2d\u51fa\u9519\uff0c\u8fd4\u56de true \u3002\u4f8b\u5982\uff1a\u5f53\u6211\u4eec\u8981\u5bf9\u4e00\u4e2a\u4e0d\u662f\u6253\u5f00\u4e3a\u5199\u72b6\u6001\u7684\u6587\u4ef6\u8fdb\u884c\u5199\u5165\u65f6\uff0c\u6216\u8005\u6211\u4eec\u8981\u5199\u5165\u7684\u8bbe\u5907\u6ca1\u6709\u5269\u4f59\u7a7a\u95f4\u7684\u65f6\u5019\u3002<\/li><li><strong>fail()<\/strong>\u9664\u4e86\u4e0ebad() \u540c\u6837\u7684\u60c5\u51b5\u4e0b\u4f1a\u8fd4\u56de true \u4ee5\u5916\uff0c\u52a0\u4e0a\u683c\u5f0f\u9519\u8bef\u65f6\u4e5f\u8fd4\u56detrue \uff0c\u4f8b\u5982\u5f53\u60f3\u8981\u8bfb\u5165\u4e00\u4e2a\u6574\u6570\uff0c\u800c\u83b7\u5f97\u4e86\u4e00\u4e2a\u5b57\u6bcd\u7684\u65f6\u5019\u3002<\/li><li><strong>eof()<\/strong>\u5982\u679c\u8bfb\u6587\u4ef6\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e\uff0c\u8fd4\u56detrue\u3002<\/li><li><strong>good()<\/strong>\u8fd9\u662f\u6700\u901a\u7528\u7684\uff1a\u5982\u679c\u8c03\u7528\u4ee5\u4e0a\u4efb\u4f55\u4e00\u4e2a\u51fd\u6570\u8fd4\u56detrue \u7684\u8bdd\uff0c\u6b64\u51fd\u6570\u8fd4\u56de false \u3002<\/li><\/ul>\n\n\n\n<p>\u8981\u60f3\u91cd\u7f6e\u4ee5\u4e0a\u6210\u5458\u51fd\u6570\u6240\u68c0\u67e5\u7684\u72b6\u6001\u6807\u5fd7\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6210\u5458\u51fd\u6570clear()\uff0c\u6ca1\u6709\u53c2\u6570\u3002<\/p>\n\n\n\n<h3>\u83b7\u5f97\u548c\u8bbe\u7f6e\u6d41\u6307\u9488(get and put stream pointers)<\/h3>\n\n\n\n<p>\u6240\u6709\u8f93\u5165\/\u8f93\u51fa\u6d41\u5bf9\u8c61(i\/o streams objects)\u90fd\u6709\u81f3\u5c11\u4e00\u4e2a\u6d41\u6307\u9488\uff1a<\/p>\n\n\n\n<ul><li>ifstream\uff0c \u7c7b\u4f3cistream, \u6709\u4e00\u4e2a\u88ab\u79f0\u4e3aget pointer\u7684\u6307\u9488\uff0c\u6307\u5411\u4e0b\u4e00\u4e2a\u5c06\u88ab\u8bfb\u53d6\u7684\u5143\u7d20\u3002<\/li><li>ofstream, \u7c7b\u4f3c ostream, \u6709\u4e00\u4e2a\u6307\u9488 put pointer \uff0c\u6307\u5411\u5199\u5165\u4e0b\u4e00\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\u3002<\/li><li>fstream, \u7c7b\u4f3c iostream, \u540c\u65f6\u7ee7\u627f\u4e86get \u548c put<\/li><\/ul>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4ee5\u4e0b\u6210\u5458\u51fd\u6570\u6765\u8bfb\u51fa\u6216\u914d\u7f6e\u8fd9\u4e9b\u6307\u5411\u6d41\u4e2d\u8bfb\u5199\u4f4d\u7f6e\u7684\u6d41\u6307\u9488\uff1a<\/p>\n\n\n\n<ul><li><strong>tellg() \u548c tellp()<\/strong>\u8fd9\u4e24\u4e2a\u6210\u5458\u51fd\u6570\u4e0d\u7528\u4f20\u5165\u53c2\u6570\uff0c\u8fd4\u56depos_type \u7c7b\u578b\u7684\u503c(\u6839\u636eANSI-C++ \u6807\u51c6) \uff0c\u5c31\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u4ee3\u8868\u5f53\u524dget \u6d41\u6307\u9488\u7684\u4f4d\u7f6e (\u7528tellg) \u6216 put \u6d41\u6307\u9488\u7684\u4f4d\u7f6e(\u7528tellp).<\/li><li><strong>seekg() \u548cseekp()<\/strong>\u8fd9\u5bf9\u51fd\u6570\u5206\u522b\u7528\u6765\u6539\u53d8\u6d41\u6307\u9488get \u548cput\u7684\u4f4d\u7f6e\u3002\u4e24\u4e2a\u51fd\u6570\u90fd\u88ab\u91cd\u8f7d\u4e3a\u4e24\u79cd\u4e0d\u540c\u7684\u539f\u578b\uff1aseekg ( pos_type position );<br>seekp ( pos_type position );\u4f7f\u7528\u8fd9\u4e2a\u539f\u578b\uff0c\u6d41\u6307\u9488\u88ab\u6539\u53d8\u4e3a\u6307\u5411\u4ece\u6587\u4ef6\u5f00\u59cb\u8ba1\u7b97\u7684\u4e00\u4e2a\u7edd\u5bf9\u4f4d\u7f6e\u3002\u8981\u6c42\u4f20\u5165\u7684\u53c2\u6570\u7c7b\u578b\u4e0e\u51fd\u6570 tellg \u548ctellp \u7684\u8fd4\u56de\u503c\u7c7b\u578b\u76f8\u540c\u3002seekg ( off_type offset, seekdir direction );<br>seekp ( off_type offset, seekdir direction );\u4f7f\u7528\u8fd9\u4e2a\u539f\u578b\u53ef\u4ee5\u6307\u5b9a\u7531\u53c2\u6570direction\u51b3\u5b9a\u7684\u4e00\u4e2a\u5177\u4f53\u7684\u6307\u9488\u5f00\u59cb\u8ba1\u7b97\u7684\u4e00\u4e2a\u4f4d\u79fb(offset)\u3002\u5b83\u53ef\u4ee5\u662f\uff1aios::beg\u4ece\u6d41\u5f00\u59cb\u4f4d\u7f6e\u8ba1\u7b97\u7684\u4f4d\u79fbios::cur\u4ece\u6d41\u6307\u9488\u5f53\u524d\u4f4d\u7f6e\u5f00\u59cb\u8ba1\u7b97\u7684\u4f4d\u79fbios::end\u4ece\u6d41\u672b\u5c3e\u5904\u5f00\u59cb\u8ba1\u7b97\u7684\u4f4d\u79fb<\/li><\/ul>\n\n\n\n<p>\u6d41\u6307\u9488 get \u548c put \u7684\u503c\u5bf9\u6587\u672c\u6587\u4ef6(text file)\u548c\u4e8c\u8fdb\u5236\u6587\u4ef6(binary file)\u7684\u8ba1\u7b97\u65b9\u6cd5\u90fd\u662f\u4e0d\u540c\u7684\uff0c\u56e0\u4e3a\u6587\u672c\u6a21\u5f0f\u7684\u6587\u4ef6\u4e2d\u67d0\u4e9b\u7279\u6b8a\u5b57\u7b26\u53ef\u80fd\u88ab\u4fee\u6539\u3002\u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u5efa\u8bae\u5bf9\u4ee5\u6587\u672c\u6587\u4ef6\u6a21\u5f0f\u6253\u5f00\u7684\u6587\u4ef6\u603b\u662f\u4f7f\u7528seekg \u548c seekp\u7684\u7b2c\u4e00\u79cd\u539f\u578b\uff0c\u800c\u4e14\u4e0d\u8981\u5bf9tellg \u6216 tellp \u7684\u8fd4\u56de\u503c\u8fdb\u884c\u4fee\u6539\u3002\u5bf9\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u4efb\u610f\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u5e94\u8be5\u4e0d\u4f1a\u6709\u4efb\u4f55\u610f\u5916\u7684\u884c\u4e3a\u4ea7\u751f\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u4f8b\u5b50\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u6765\u83b7\u5f97\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u5927\u5c0f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ obtaining file size #include &lt;iostream.h&gt; #include &lt;fstream.h&gt; const char * filename = &#8220;example.txt&#8221;; int main () { long l,m; ifstream file (filename, ios::in|ios::binary); l = file.tellg(); file.seekg (0, ios::end); m = file.tellg(); file.close(); cout &lt;&lt; &#8220;size of &#8221; &lt;&lt; filename; cout &lt;&lt; &#8221; is &#8221; &lt;&lt; (m-l) &lt;&lt; &#8221; bytes.\\n&#8221;; return 0; }<\/td><td>size of example.txt is 40 bytes.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3>\u4e8c\u8fdb\u5236\u6587\u4ef6(Binary files)<\/h3>\n\n\n\n<p>\u5728\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528&lt;&lt; \u548c&gt;&gt;\uff0c\u4ee5\u53ca\u51fd\u6570\uff08\u5982getline\uff09\u6765\u64cd\u4f5c\u7b26\u8f93\u5165\u548c\u8f93\u51fa\u6570\u636e\uff0c\u6ca1\u6709\u4ec0\u4e48\u5b9e\u9645\u610f\u4e49\uff0c\u867d\u7136\u5b83\u4eec\u662f\u7b26\u5408\u8bed\u6cd5\u7684\u3002<\/p>\n\n\n\n<p>\u6587\u4ef6\u6d41\u5305\u62ec\u4e24\u4e2a\u4e3a\u987a\u5e8f\u8bfb\u5199\u6570\u636e\u7279\u6b8a\u8bbe\u8ba1\u7684\u6210\u5458\u51fd\u6570\uff1awrite \u548c read\u3002\u7b2c\u4e00\u4e2a\u51fd\u6570 (write) \u662fostream \u7684\u4e00\u4e2a\u6210\u5458\u51fd\u6570\uff0c\u90fd\u662f\u88abofstream\u6240\u7ee7\u627f\u3002\u800cread \u662fistream \u7684\u4e00\u4e2a\u6210\u5458\u51fd\u6570\uff0c\u88abifstream \u6240\u7ee7\u627f\u3002\u7c7b fstream \u7684\u5bf9\u8c61\u540c\u65f6\u62e5\u6709\u8fd9\u4e24\u4e2a\u51fd\u6570\u3002\u5b83\u4eec\u7684\u539f\u578b\u662f\uff1awrite ( char * buffer, streamsize size );<br>read ( char * buffer, streamsize size );<\/p>\n\n\n\n<p>\u8fd9\u91cc buffer \u662f\u4e00\u5757\u5185\u5b58\u7684\u5730\u5740\uff0c\u7528\u6765\u5b58\u50a8\u6216\u8bfb\u51fa\u6570\u636e\u3002\u53c2\u6570size \u662f\u4e00\u4e2a\u6574\u6570\u503c\uff0c\u8868\u793a\u8981\u4ece\u7f13\u5b58\uff08buffer\uff09\u4e2d\u8bfb\u51fa\u6216\u5199\u5165\u7684\u5b57\u7b26\u6570\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ reading binary file #include &lt;iostream&gt; #include &lt;fstream.h&gt; const char * filename = &#8220;example.txt&#8221;; int main () { char * buffer; long size; ifstream file (filename, ios::in|ios::binary|ios::ate); size = file.tellg(); file.seekg (0, ios::beg); buffer = new char [size]; file.read (buffer, size); file.close(); cout &lt;&lt; &#8220;the complete file is in a buffer&#8221;; delete[] buffer; return 0; }<\/td><td>The complete file is in a buffer<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3>\u7f13\u5b58\u548c\u540c\u6b65(Buffers and Synchronization)<\/h3>\n\n\n\n<p>\u5f53\u6211\u4eec\u5bf9\u6587\u4ef6\u6d41\u8fdb\u884c\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u5b83\u4eec\u4e0e\u4e00\u4e2astreambuf \u7c7b\u578b\u7684\u7f13\u5b58(buffer)\u8054\u7cfb\u5728\u4e00\u8d77\u3002\u8fd9\u4e2a\u7f13\u5b58\uff08buffer\uff09\u5b9e\u9645\u662f\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\uff0c\u4f5c\u4e3a\u6d41(stream)\u548c\u7269\u7406\u6587\u4ef6\u7684\u5a92\u4ecb\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u8f93\u51fa\u6d41\uff0c\u6bcf\u6b21\u6210\u5458\u51fd\u6570put (\u5199\u4e00\u4e2a\u5355\u4e2a\u5b57\u7b26)\u88ab\u8c03\u7528\uff0c\u8fd9\u4e2a\u5b57\u7b26\u4e0d\u662f\u76f4\u63a5\u88ab\u5199\u5165\u8be5\u8f93\u51fa\u6d41\u6240\u5bf9\u5e94\u7684\u7269\u7406\u6587\u4ef6\u4e2d\u7684\uff0c\u800c\u662f\u9996\u5148\u88ab\u63d2\u5165\u5230\u8be5\u6d41\u7684\u7f13\u5b58\uff08buffer\uff09\u4e2d\u3002<\/p>\n\n\n\n<p>\u5f53\u7f13\u5b58\u88ab\u6392\u653e\u51fa\u6765(flush)\u65f6\uff0c\u5b83\u91cc\u9762\u7684\u6240\u6709\u6570\u636e\u6216\u8005\u88ab\u5199\u5165\u7269\u7406\u5a92\u8d28\u4e2d\uff08\u5982\u679c\u662f\u4e00\u4e2a\u8f93\u51fa\u6d41\u7684\u8bdd\uff09\uff0c\u6216\u8005\u7b80\u5355\u7684\u88ab\u62b9\u6389(\u5982\u679c\u662f\u4e00\u4e2a\u8f93\u5165\u6d41\u7684\u8bdd)\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u540c\u6b65(synchronization)\uff0c\u5b83\u4f1a\u5728\u4ee5\u4e0b\u4efb\u4e00\u60c5\u51b5\u4e0b\u53d1\u751f\uff1a<\/p>\n\n\n\n<ul><li><strong>\u5f53\u6587\u4ef6\u88ab\u5173\u95ed\u65f6:<\/strong>&nbsp;\u5728\u6587\u4ef6\u88ab\u5173\u95ed\u4e4b\u524d\uff0c\u6240\u6709\u8fd8\u6ca1\u6709\u88ab\u5b8c\u5168\u5199\u51fa\u6216\u8bfb\u53d6\u7684\u7f13\u5b58\u90fd\u5c06\u88ab\u540c\u6b65\u3002<\/li><li><strong>\u5f53\u7f13\u5b58buffer \u6ee1\u65f6:<\/strong>\u7f13\u5b58Buffers \u6709\u4e00\u5b9a\u7684\u7a7a\u95f4\u9650\u5236\u3002\u5f53\u7f13\u5b58\u6ee1\u65f6\uff0c\u5b83\u4f1a\u88ab\u81ea\u52a8\u540c\u6b65\u3002<\/li><li><strong>\u63a7\u5236\u7b26\u660e\u786e\u6307\u660e:<\/strong>\u5f53\u9047\u5230\u6d41\u4e2d\u67d0\u4e9b\u7279\u5b9a\u7684\u63a7\u5236\u7b26\u65f6\uff0c\u540c\u6b65\u4f1a\u53d1\u751f\u3002\u8fd9\u4e9b\u63a7\u5236\u7b26\u5305\u62ec\uff1aflush \u548cendl\u3002<\/li><li><strong>\u660e\u786e\u8c03\u7528\u51fd\u6570sync():<\/strong>&nbsp;\u8c03\u7528\u6210\u5458\u51fd\u6570sync() (\u65e0\u53c2\u6570)\u53ef\u4ee5\u5f15\u53d1\u7acb\u5373\u540c\u6b65\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2aint \u503c\uff0c\u7b49\u4e8e-1 \u8868\u793a\u6d41\u6ca1\u6709\u8054\u7cfb\u7684\u7f13\u5b58\u6216\u64cd\u4f5c\u5931\u8d25\u3002<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>6.1 \u6587\u4ef6\u7684\u8f93\u5165\u8f93\u51fa (Input\/Output with files) C++ \u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u7c7b\u652f\u6301\u6587\u4ef6\u7684\u8f93\u5165\u8f93\u51fa\uff1a ofstream: \u5199\u64cd\u4f5c\uff08\u8f93\u51fa\uff09\u7684\u6587\u4ef6\u7c7b (\u7531ostream\u5f15\u7533\u800c\u6765) ifstream: \u8bfb\u64cd\u4f5c\uff08\u8f93\u5165\uff09\u7684\u6587\u4ef6\u7c7b(\u7531istream\u5f15\u7533\u800c\u6765) fstream: \u53ef\u540c\u65f6\u8bfb\u5199\u64cd\u4f5c\u7684\u6587\u4ef6\u7c7b (\u7531iostream\u5f15\u7533\u800c\u6765) \u6253\u5f00\u6587\u4ef6(Open a file) \u5bf9\u8fd9\u4e9b\u7c7b\u7684\u4e00\u4e2a\u5bf9\u8c61\u6240\u505a\u7684\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u901a\u5e38\u5c31\u662f\u5c06\u5b83\u548c\u4e00\u4e2a\u771f\u6b63\u7684\u6587\u4ef6\u8054\u7cfb\u8d77\u6765\uff0c\u4e5f\u5c31\u662f\u8bf4\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u3002\u88ab\u6253\u5f00\u7684\u6587\u4ef6\u5728\u7a0b\u5e8f\u4e2d\u7531\u4e00\u4e2a\u6d41\u5bf9\u8c61(stream object)\u6765\u8868\u793a (\u8fd9\u4e9b\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b) \uff0c\u800c\u5bf9\u8fd9\u4e2a\u6d41\u5bf9\u8c61\u6240\u505a\u7684\u4efb\u4f55\u8f93\u5165\u8f93\u51fa\u64cd\u4f5c\u5b9e\u9645\u5c31\u662f\u5bf9\u8be5\u6587\u4ef6\u6240\u505a\u7684\u64cd\u4f5c\u3002 \u8981\u901a\u8fc7\u4e00\u4e2a\u6d41\u5bf9\u8c61\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u6211\u4eec\u4f7f\u7528\u5b83\u7684\u6210\u5458\u51fd\u6570open()\uff1avoid open (const char * filename, openmode mode); \u8fd9\u91ccfilename \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4ee3\u8868\u8981\u6253\u5f00\u7684\u6587\u4ef6\u540d\uff0cmode \u662f\u4ee5\u4e0b\u6807\u5fd7\u7b26\u7684\u4e00\u4e2a\u7ec4\u5408\uff1a ios::in \u4e3a\u8f93\u5165(\u8bfb)\u800c\u6253\u5f00\u6587\u4ef6 ios::out \u4e3a\u8f93\u51fa(\u5199)\u800c\u6253\u5f00\u6587\u4ef6 ios::ate \u521d\u59cb\u4f4d\u7f6e\uff1a\u6587\u4ef6\u5c3e ios::app \u6240\u6709\u8f93\u51fa\u9644\u52a0\u5728\u6587\u4ef6\u672b\u5c3e ios::trunc \u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\u5219\u5148\u5220\u9664\u8be5\u6587\u4ef6 ios::binary \u4e8c\u8fdb\u5236\u65b9\u5f0f \u8fd9\u4e9b\u6807\u8bc6\u7b26\u53ef\u4ee5\u88ab\u7ec4\u5408\u4f7f\u7528\uff0c\u4e2d\u95f4\u4ee5\u201d\u6216\u201d\u64cd\u4f5c\u7b26(|)\u95f4\u9694\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u4ee5\u4e8c\u8fdb\u5236\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6&#8221;example.bin&#8221; \u6765\u5199\u5165\u4e00\u4e9b\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8c03\u7528\u6210\u5458\u51fd\u6570open\uff08\uff09\u6765\u5b9e\u73b0\uff1aofstream file;file.open (&#8220;example.bin&#8221;, ios::out | ios::app | ios::binary); ofstream, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":375,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/377"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=377"}],"version-history":[{"count":1,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/377\/revisions"}],"predecessor-version":[{"id":378,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/377\/revisions\/378"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/375"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}