{"id":3316,"date":"2025-03-29T17:11:13","date_gmt":"2025-03-29T09:11:13","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3316"},"modified":"2025-03-29T17:11:13","modified_gmt":"2025-03-29T09:11:13","slug":"c-%e6%96%87%e4%bb%b6%e5%92%8c%e6%b5%81","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/29\/c-%e6%96%87%e4%bb%b6%e5%92%8c%e6%b5%81\/","title":{"rendered":"C++ \u6587\u4ef6\u548c\u6d41"},"content":{"rendered":"\n<p>\u5728 C++ \u4e2d\uff0c\u6587\u4ef6\u548c\u6d41\u662f\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u7684\u57fa\u672c\u673a\u5236\u3002\u901a\u8fc7\u6587\u4ef6\u6d41\uff08File Streams\uff09\uff0c\u53ef\u4ee5\u5bf9\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6\u3001\u5199\u5165\u4ee5\u53ca\u5176\u4ed6\u5404\u79cd\u64cd\u4f5c\u3002C++ \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 <code>fstream<\/code> \u7c7b\u5e93\u6765\u5904\u7406\u6587\u4ef6\u7684\u8f93\u5165\u8f93\u51fa\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u6587\u4ef6\u6d41\u7684\u57fa\u672c\u6982\u5ff5<\/strong><\/h2>\n\n\n\n<p>\u6587\u4ef6\u6d41\u662f\u7531 C++ \u7684 <code>fstream<\/code> \u7c7b\u63d0\u4f9b\u7684\u4e00\u79cd\u673a\u5236\uff0c\u7528\u4e8e\u5bf9\u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c\u3002\u6587\u4ef6\u6d41\u5206\u4e3a\u4e09\u7c7b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\u6587\u4ef6\u6d41\uff08ifstream\uff09<\/strong>\uff1a\u7528\u4e8e\u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\u6587\u4ef6\u6d41\uff08ofstream\uff09<\/strong>\uff1a\u7528\u4e8e\u5411\u6587\u4ef6\u5199\u5165\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u6587\u4ef6\u6d41\uff08fstream\uff09<\/strong>\uff1a\u540c\u65f6\u652f\u6301\u8bfb\u5199\u6587\u4ef6\u7684\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u6587\u4ef6\u6d41\u7684\u4f7f\u7528<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u6253\u5f00\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<p>\u5728 C++ \u4e2d\uff0c\u6587\u4ef6\u6d41\u5bf9\u8c61\u9700\u8981\u901a\u8fc7 <code>open()<\/code> \u65b9\u6cd5\u6253\u5f00\u6587\u4ef6\u3002\u6587\u4ef6\u6253\u5f00\u540e\uff0c\u5c31\u53ef\u4ee5\u8fdb\u884c\u76f8\u5e94\u7684\u8bfb\u5199\u64cd\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;fstream&gt;\nusing namespace std;\n\nint main() {\n    \/\/ \u521b\u5efa\u4e00\u4e2a ofstream \u5bf9\u8c61\uff08\u8f93\u51fa\u6587\u4ef6\u6d41\uff09\n    ofstream outFile;\n\n    \/\/ \u6253\u5f00\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u6587\u4ef6\n    outFile.open(\"example.txt\");\n\n    \/\/ \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u6210\u529f\u6253\u5f00\n    if (!outFile) {\n        cerr &lt;&lt; \"Error opening file for writing!\" &lt;&lt; endl;\n        return 1;\n    }\n\n    \/\/ \u5411\u6587\u4ef6\u5199\u5165\u6570\u636e\n    outFile &lt;&lt; \"Hello, C++ file operations!\" &lt;&lt; endl;\n\n    \/\/ \u5173\u95ed\u6587\u4ef6\n    outFile.close();\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>open()<\/code>\uff1a\u6253\u5f00\u6307\u5b9a\u7684\u6587\u4ef6\uff0c<code>ofstream<\/code> \u4f1a\u521b\u5efa\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u5df2\u7ecf\u5b58\u5728\uff0c\u4f1a\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\u3002<\/li>\n\n\n\n<li><code>close()<\/code>\uff1a\u64cd\u4f5c\u5b8c\u6210\u540e\u5173\u95ed\u6587\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e<\/strong><\/h3>\n\n\n\n<p>\u4f7f\u7528 <code>ifstream<\/code> \u7c7b\u6765\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u3002\u8bfb\u53d6\u7684\u65b9\u5f0f\u901a\u5e38\u6709\u4e24\u79cd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9010\u884c\u8bfb\u53d6<\/li>\n\n\n\n<li>\u6309\u5355\u4e2a\u5b57\u7b26\u6216\u683c\u5f0f\u8bfb\u53d6<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.2.1 \u9010\u884c\u8bfb\u53d6<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;fstream&gt;\n#include &lt;string&gt;\nusing namespace std;\n\nint main() {\n    \/\/ \u521b\u5efa ifstream \u5bf9\u8c61\uff08\u8f93\u5165\u6587\u4ef6\u6d41\uff09\n    ifstream inFile(\"example.txt\");\n\n    \/\/ \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u6210\u529f\u6253\u5f00\n    if (!inFile) {\n        cerr &lt;&lt; \"Error opening file for reading!\" &lt;&lt; endl;\n        return 1;\n    }\n\n    string line;\n\n    \/\/ \u9010\u884c\u8bfb\u53d6\u6587\u4ef6\n    while (getline(inFile, line)) {\n        cout &lt;&lt; line &lt;&lt; endl;\n    }\n\n    \/\/ \u5173\u95ed\u6587\u4ef6\n    inFile.close();\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.2.2 \u6309\u5b57\u7b26\u8bfb\u53d6<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;fstream&gt;\nusing namespace std;\n\nint main() {\n    \/\/ \u521b\u5efa ifstream \u5bf9\u8c61\n    ifstream inFile(\"example.txt\");\n\n    \/\/ \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u6210\u529f\u6253\u5f00\n    if (!inFile) {\n        cerr &lt;&lt; \"Error opening file for reading!\" &lt;&lt; endl;\n        return 1;\n    }\n\n    char ch;\n\n    \/\/ \u9010\u5b57\u7b26\u8bfb\u53d6\u6587\u4ef6\n    while (inFile.get(ch)) {\n        cout &lt;&lt; ch;\n    }\n\n    \/\/ \u5173\u95ed\u6587\u4ef6\n    inFile.close();\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3 \u5199\u5165\u6570\u636e\u5230\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<p>\u4e0e\u8bfb\u53d6\u6587\u4ef6\u7c7b\u4f3c\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>ofstream<\/code> \u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u5199\u5165\u6570\u636e\u65f6\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5199\u5165\u5b57\u7b26\u4e32<\/li>\n\n\n\n<li>\u5199\u5165\u6570\u5b57<\/li>\n\n\n\n<li>\u5199\u5165\u683c\u5f0f\u5316\u6570\u636e<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.3.1 \u5199\u5165\u5b57\u7b26\u4e32\u548c\u6570\u5b57<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;fstream&gt;\nusing namespace std;\n\nint main() {\n    \/\/ \u521b\u5efa ofstream \u5bf9\u8c61\n    ofstream outFile(\"example_output.txt\");\n\n    \/\/ \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u6210\u529f\u6253\u5f00\n    if (!outFile) {\n        cerr &lt;&lt; \"Error opening file for writing!\" &lt;&lt; endl;\n        return 1;\n    }\n\n    \/\/ \u5199\u5165\u5b57\u7b26\u4e32\u548c\u6570\u5b57\n    outFile &lt;&lt; \"Hello, C++ file handling!\" &lt;&lt; endl;\n    outFile &lt;&lt; \"The number is: \" &lt;&lt; 42 &lt;&lt; endl;\n\n    \/\/ \u5173\u95ed\u6587\u4ef6\n    outFile.close();\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.4 \u8bfb\u5199\u6587\u4ef6\uff08fstream\uff09<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u8981\u540c\u65f6\u8bfb\u5199\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>fstream<\/code> \u7c7b\u3002\u8fd9\u4e2a\u7c7b\u7ee7\u627f\u81ea <code>ifstream<\/code> \u548c <code>ofstream<\/code>\uff0c\u652f\u6301\u4e24\u8005\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;fstream&gt;\nusing namespace std;\n\nint main() {\n    \/\/ \u521b\u5efa fstream \u5bf9\u8c61\uff08\u65e2\u53ef\u4ee5\u8bfb\u53d6\u4e5f\u53ef\u4ee5\u5199\u5165\uff09\n    fstream file;\n\n    \/\/ \u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\n    file.open(\"example.txt\", ios::in | ios::out | ios::app);\n\n    \/\/ \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u6210\u529f\u6253\u5f00\n    if (!file) {\n        cerr &lt;&lt; \"Error opening file for reading\/writing!\" &lt;&lt; endl;\n        return 1;\n    }\n\n    \/\/ \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\n    string content;\n    while (getline(file, content)) {\n        cout &lt;&lt; content &lt;&lt; endl;\n    }\n\n    \/\/ \u5728\u6587\u4ef6\u672b\u5c3e\u5199\u5165\u5185\u5bb9\n    file &lt;&lt; \"\\nAppending new content to the file.\";\n\n    \/\/ \u5173\u95ed\u6587\u4ef6\n    file.close();\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.5 \u6587\u4ef6\u6d41\u6a21\u5f0f<\/strong><\/h3>\n\n\n\n<p><code>fstream<\/code> \u548c <code>ofstream<\/code> \u652f\u6301\u4e0d\u540c\u7684\u6587\u4ef6\u6253\u5f00\u6a21\u5f0f\u3002\u5e38\u89c1\u7684\u6a21\u5f0f\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ios::in<\/strong>\uff1a\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6\u3002<\/li>\n\n\n\n<li><strong>ios::out<\/strong>\uff1a\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u5199\u5165\u3002<\/li>\n\n\n\n<li><strong>ios::app<\/strong>\uff1a\u6253\u5f00\u6587\u4ef6\u5e76\u5c06\u6570\u636e\u8ffd\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\u3002<\/li>\n\n\n\n<li><strong>ios::ate<\/strong>\uff1a\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u5c06\u6587\u4ef6\u6307\u9488\u79fb\u52a8\u5230\u6587\u4ef6\u7684\u672b\u5c3e\u3002<\/li>\n\n\n\n<li><strong>ios::trunc<\/strong>\uff1a\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u5219\u6587\u4ef6\u5185\u5bb9\u4f1a\u88ab\u6e05\u7a7a\uff08\u9ed8\u8ba4\u884c\u4e3a\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>ofstream outFile(\"example.txt\", ios::out | ios::trunc);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u6587\u4ef6\u64cd\u4f5c\u7684\u9519\u8bef\u5904\u7406<\/strong><\/h2>\n\n\n\n<p>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u52a1\u5fc5\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u6210\u529f\u6253\u5f00\u3002\u53ef\u4ee5\u4f7f\u7528 <code>fail()<\/code> \u65b9\u6cd5\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u6253\u5f00\u6210\u529f\uff0c\u6216\u8005\u68c0\u67e5\u6587\u4ef6\u6d41\u7684\u72b6\u6001\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ifstream inFile(\"example.txt\");\n\nif (inFile.fail()) {\n    cerr &lt;&lt; \"Error opening file!\" &lt;&lt; endl;\n    return 1;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u6587\u4ef6\u6307\u9488\u64cd\u4f5c<\/strong><\/h2>\n\n\n\n<p>\u6587\u4ef6\u6d41\u5bf9\u8c61\u7ef4\u62a4\u7740\u4e00\u4e2a\u5185\u90e8\u7684\u6587\u4ef6\u6307\u9488\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u65b9\u6cd5\u6765\u64cd\u4f5c\u6587\u4ef6\u6307\u9488\u7684\u4f4d\u7f6e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>seekg()<\/strong>\uff1a\u8bbe\u7f6e\u8f93\u5165\u6587\u4ef6\u6d41\u7684\u6307\u9488\u4f4d\u7f6e\uff08\u8bfb\u53d6\uff09\u3002<\/li>\n\n\n\n<li><strong>seekp()<\/strong>\uff1a\u8bbe\u7f6e\u8f93\u51fa\u6587\u4ef6\u6d41\u7684\u6307\u9488\u4f4d\u7f6e\uff08\u5199\u5165\uff09\u3002<\/li>\n\n\n\n<li><strong>tellg()<\/strong>\uff1a\u83b7\u53d6\u5f53\u524d\u8f93\u5165\u6d41\u7684\u6307\u9488\u4f4d\u7f6e\u3002<\/li>\n\n\n\n<li><strong>tellp()<\/strong>\uff1a\u83b7\u53d6\u5f53\u524d\u8f93\u51fa\u6d41\u7684\u6307\u9488\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>fstream file(\"example.txt\", ios::in | ios::out);\n\n\/\/ \u5c06\u6587\u4ef6\u6307\u9488\u79fb\u5230\u6587\u4ef6\u5f00\u5934\nfile.seekg(0, ios::beg);\n\n\/\/ \u83b7\u53d6\u6587\u4ef6\u5f53\u524d\u6307\u9488\u4f4d\u7f6e\ncout &lt;&lt; \"Current position: \" &lt;&lt; file.tellg() &lt;&lt; endl;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u5728 C++ \u4e2d\uff0c\u6587\u4ef6\u64cd\u4f5c\u662f\u901a\u8fc7\u6587\u4ef6\u6d41\uff08<code>fstream<\/code>, <code>ifstream<\/code>, <code>ofstream<\/code>\uff09\u6765\u5b9e\u73b0\u7684\u3002\u901a\u8fc7\u8fd9\u4e9b\u7c7b\uff0c\u4f60\u53ef\u4ee5\u8bfb\u53d6\u3001\u5199\u5165\u548c\u4fee\u6539\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u3002\u4e86\u89e3\u5e76\u719f\u7ec3\u638c\u63e1\u6587\u4ef6\u6d41\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u5f97\u4f60\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\u5b58\u50a8\u4e0e\u8bfb\u53d6\u4efb\u52a1\u3002<\/p>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u7684\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 C++ \u4e2d\uff0c\u6587\u4ef6\u548c\u6d41\u662f\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u7684\u57fa\u672c\u673a\u5236\u3002\u901a\u8fc7\u6587\u4ef6\u6d41\uff08File Streams\uff09\uff0c\u53ef\u4ee5\u5bf9\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6\u3001 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3317,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81],"tags":[],"class_list":["post-3316","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3316"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3316\/revisions"}],"predecessor-version":[{"id":3318,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3316\/revisions\/3318"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3317"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}