{"id":231,"date":"2025-08-05T17:28:24","date_gmt":"2025-08-05T09:28:24","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=231"},"modified":"2025-11-24T16:39:22","modified_gmt":"2025-11-24T08:39:22","slug":"io%e5%9f%ba%e7%a1%80%e5%8f%8a%e5%ba%93%e5%87%bd%e6%95%b0%e7%9a%84%e7%ae%80%e5%8d%95%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=231","title":{"rendered":"IO\u57fa\u7840\u53ca\u5e93\u51fd\u6570\u7684\u7b80\u5355\u5b9e\u73b0"},"content":{"rendered":"\n<p>\u4ee3\u7801\u4ed3\u5e93\u5730\u5740\uff1a<a href=\"https:\/\/gitee.com\/tgwTTT\/linux-learning-dai\/tree\/master\/glibc\">https:\/\/gitee.com\/tgwTTT\/linux-learning-dai\/tree\/master\/glibc<\/a><\/p>\n\n\n\n<p><strong>\u6587\u4ef6\uff1a<\/strong><\/p>\n\n\n\n<p>1.\u72ed\u4e49\u7406\u89e3\uff1a<\/p>\n\n\n\n<p>\u2022 \u2f42\u4ef6\u5728\u78c1\u76d8\u2fa5<\/p>\n\n\n\n<p>\u2022 \u78c1\u76d8\u662f\u6c38\u4e45\u6027\u5b58\u50a8\u4ecb\u8d28\uff0c\u56e0\u6b64\u2f42\u4ef6\u5728\u78c1\u76d8\u4e0a\u7684\u5b58\u50a8\u662f\u6c38\u4e45\u6027\u7684<\/p>\n\n\n\n<p>\u2022 \u78c1\u76d8\u662f\u5916\u8bbe\uff08\u5373\u662f\u8f93\u51fa\u8bbe\u5907\u4e5f\u662f\u8f93\u2f0a\u8bbe\u5907\uff09<\/p>\n\n\n\n<p>\u2022 \u78c1\u76d8\u4e0a\u7684\u2f42\u4ef6 \u672c\u8d28\u662f\u5bf9\u2f42\u4ef6\u7684\u6240\u6709\u64cd\u4f5c\uff0c\u90fd\u662f\u5bf9\u5916\u8bbe\u7684\u8f93\u2f0a\u548c\u8f93\u51fa \u7b80\u79f0 IO<\/p>\n\n\n\n<p>2.\u5e7f\u4e49\u7406\u89e3\uff1a<\/p>\n\n\n\n<p>Linux \u4e0b\u2f00\u5207\u7686\u2f42\u4ef6\uff08\u952e\u76d8\u3001\u663e\u2f70\u5668\u3001\u2f79\u5361\u3001\u78c1\u76d8\u2026\u2026 \u8fd9\u4e9b\u90fd\u662f\u62bd\u8c61\u5316\u7684\u8fc7\u7a0b\uff09<\/p>\n\n\n\n<p>3.\u6587\u4ef6\u7279\u70b9\uff1a<\/p>\n\n\n\n<p>\u2022 \u5bf9\u4e8e 0KB \u7684\u7a7a\u2f42\u4ef6\u662f\u5360\u2f64\u78c1\u76d8\u7a7a\u95f4\u7684 \u2022 <\/p>\n\n\n\n<p>\u2f42\u4ef6\u662f\u2f42\u4ef6\u5c5e\u6027\uff08\u5143\u6570\u636e\uff09\u548c\u2f42\u4ef6\u5185\u5bb9\u7684\u96c6\u5408\uff08\u2f42\u4ef6 = \u5c5e\u6027\uff08\u5143\u6570\u636e\uff09+ \u5185\u5bb9\uff09 <\/p>\n\n\n\n<p>\u2022 \u6240\u6709\u7684\u2f42\u4ef6\u64cd\u4f5c\u672c\u8d28\u662f\u2f42\u4ef6\u5185\u5bb9\u64cd\u4f5c\u548c\u2f42\u4ef6\u5c5e\u6027\u64cd\u4f5c<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4e0b\u9762\u4ee5c\u6587\u4ef6\u63a5\u53e3\u4e3a\u4f8b\u53bb\u4ecb\u7ecdIO<\/p>\n\n\n\n<p>c\u6587\u4ef6\u63a5\u53e3\uff1a<\/p>\n\n\n\n<p>fwrite()<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"620\" height=\"122\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image.png\" alt=\"\" class=\"wp-image-232\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image.png 620w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-300x59.png 300w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/figure>\n\n\n\n<p>fread()<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"593\" height=\"131\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-1.png\" alt=\"\" class=\"wp-image-233\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-1.png 593w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-1-300x66.png 300w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><\/figure>\n\n\n\n<p>\u9664\u6b64\u4e4b\u5916\u4f1a\u9ed8\u8ba4\u6253\u5f00\u4e09\u4e2a\u8f93\u2f0a\u8f93\u51fa\u6d41stdin &#038; stdout &#038; stderr<\/p>\n\n\n\n<p><strong>\u63a5\u4e0b\u6765\u662flinux\u7cfb\u7edfIO\u63a5\u53e3\u4ecb\u7ecd<\/strong><\/p>\n\n\n\n<p>\u6253\u5f00\u2f42\u4ef6\u7684\u2f45\u5f0f\u4e0d\u4ec5\u4ec5\u662ffopen\uff0cifstream\u7b49\u6d41\u5f0f\uff0c\u8bed\u2f94\u5c42\u7684\u2f45\u6848\uff0c\u5176\u5b9e\u7cfb\u7edf\u624d\u662f\u6253\u5f00\u2f42\u4ef6\u6700\u5e95\u5c42\u7684\u2f45\u6848\u3002\u4e0d\u8fc7\uff0c\u5728\u5b66\u4e60\u7cfb\u7edf\u2f42\u4ef6IO\u4e4b\u524d\uff0c\u5148\u8981\u4e86\u89e3\u4e0b\u5982\u4f55\u7ed9\u51fd\u6570\u4f20\u9012\u6807\u5fd7\u4f4d\uff0c\u8be5\u2f45\u6cd5\u5728\u7cfb\u7edf\u2f42\u4ef6IO\u63a5\u2f1d\u4e2d\u4f1a\u4f7f\u2f64\u5230\uff1a<\/p>\n\n\n\n<p>\u4f8b\u5982\u8fd9\u79cd\u65b9\u5f0f\uff1a<br><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include <stdio.h>\n#define ONE 0001 \/\/0000 0001\n#define TWO 0002 \/\/0000 0010\n#define THREE 0004 \/\/0000 0100\nvoid func(int flags) {\n\tif (flags & ONE) printf(\"flags has ONE! \");\n\tif (flags & TWO) printf(\"flags has TWO! \");\n\tif (flags & THREE) printf(\"flags has THREE! \");\n\tprintf(\"\\n\");\n} \nint main() {\n\tfunc(ONE);\n\tfunc(THREE);\n\tfunc(ONE | TWO);\n\tfunc(ONE | THREE | TWO);\n\treturn 0;\n}\n<\/code><\/pre>\n\n\n\n<p><strong>open\u51fd\u6570\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"581\" height=\"130\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-2.png\" alt=\"\" class=\"wp-image-234\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-2.png 581w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-2-300x67.png 300w\" sizes=\"auto, (max-width: 581px) 100vw, 581px\" \/><\/figure>\n\n\n\n<p>pathname: \u8981\u6253\u5f00\u6216\u521b\u5efa\u7684\u2f6c\u6807\u2f42\u4ef6<\/p>\n\n\n\n<p>flags: \u6253\u5f00\u2f42\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f20\u2f0a\u591a\u4e2a\u53c2\u6570\u9009\u9879\uff0c\u2f64\u4e0b\u2faf\u7684\u2f00\u4e2a\u6216\u8005\u591a\u4e2a\u5e38\u91cf\u8fdb\u2f8f\u201c\u6216\u201d\u8fd0\u7b97\uff0c\u6784\u6210flags\u3002<\/p>\n\n\n\n<p>write\u51fd\u6570\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"503\" height=\"79\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-3.png\" alt=\"\" class=\"wp-image-235\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-3.png 503w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-3-300x47.png 300w\" sizes=\"auto, (max-width: 503px) 100vw, 503px\" \/><\/figure>\n\n\n\n<p>\u6587\u4ef6\u63cf\u8ff0\u7b26fd<\/p>\n\n\n\n<p>\u6587\u4ef6\u6570\u7ec4buf<\/p>\n\n\n\n<p>\u5927\u5c0fcount<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u7528\u7cfb\u7edf\u51fd\u6570\u8c03\u7528\u5b9e\u73b0c\u5e93\u4e2d\u7684\u51fd\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include <cstring>\n#include <cstdlib>\n#include <cstdio>\n#include <fcntl.h>\n#include <unistd.h>\n\n\/\/ \u5237\u65b0\u65b9\u5f0f\n#define LINE_FLUSH 0x1\n\n\/\/ \u81ea\u5b9a\u4e49 FILE \u7ed3\u6784\nstruct MYFILE {\n    int fileno;               \/\/ \u6587\u4ef6\u63cf\u8ff0\u7b26\n    int flag;                 \/\/ \u6253\u5f00\u6807\u5fd7\n    int bufferlen;            \/\/ \u7f13\u51b2\u533a\u4e2d\u5df2\u7528\u5b57\u8282\u6570\n    char outbuffer[1024];     \/\/ \u8f93\u51fa\u7f13\u51b2\u533a\n    int flush_method;         \/\/ \u5237\u65b0\u7b56\u7565\n};\n\n\/\/ \u521b\u5efa\u5e76\u521d\u59cb\u5316 MYFILE \u5bf9\u8c61\nstatic MYFILE* BuyFile(int fd, int flag) {\n    MYFILE* f = new MYFILE();\n    if (f == nullptr) return nullptr;\n\n    f->bufferlen = 0;\n    f->fileno = fd;\n    f->flag = flag;\n    f->flush_method = LINE_FLUSH;\n    memset(f->outbuffer, 0, sizeof(f->outbuffer));\n\n    return f;\n}\n\n\/\/ \u81ea\u5b9a\u4e49 fopen\nMYFILE* MYFopen(const char* path, const char* mode) {\n    int fd = -1;\n    int flag = 0;\n\n    if (strcmp(mode, \"w\") == 0) {\n        flag = O_CREAT | O_WRONLY | O_TRUNC;\n        fd = open(path, flag, 0666);\n    }\n    else if (strcmp(mode, \"a\") == 0) {\n        flag = O_CREAT | O_WRONLY | O_APPEND;\n        fd = open(path, flag, 0666);\n    }\n    else if (strcmp(mode, \"r\") == 0) {\n        flag = O_RDONLY;\n        fd = open(path, flag);\n    }\n    else {\n        \/\/ \u975e\u6cd5\u6a21\u5f0f\n        return nullptr;\n    }\n\n    if (fd < 0) return nullptr;\n\n    return BuyFile(fd, flag);\n}\n\n\/\/ \u81ea\u5b9a\u4e49 fclose\nvoid MYFclose(MYFILE* file) {\n    if (file) {\n        MYFflush(file);  \/\/ \u5173\u95ed\u524d\u5237\u65b0\n        close(file->fileno);\n        delete file;\n    }\n}\n\n\/\/ \u81ea\u5b9a\u4e49 fflush\nvoid MYFflush(MYFILE* file) {\n    if (file == nullptr || file->bufferlen == 0) return;\n\n    ssize_t n = write(file->fileno, file->outbuffer, file->bufferlen);\n    if (n < 0) {\n        perror(\"write failed\");\n    }\n    file->bufferlen = 0;\n}\n\n\/\/ \u81ea\u5b9a\u4e49 fwrite\nint MYFwrite(MYFILE* file, const void* str, int len) {\n    if (file == nullptr || str == nullptr || len <= 0) return 0;\n\n    \/\/ \u5982\u679c\u7f13\u51b2\u533a\u88c5\u4e0d\u4e0b\uff0c\u5148\u5237\u65b0\n    if (file->bufferlen + len > (int)sizeof(file->outbuffer)) {\n        MYFflush(file);\n    }\n\n    \/\/ \u62f7\u8d1d\u5230\u7f13\u51b2\u533a\n    memcpy(file->outbuffer + file->bufferlen, str, len);\n    file->bufferlen += len;\n\n    \/\/ \u884c\u7f13\u51b2\uff1a\u9047\u5230 '\\n' \u5c31\u5237\u65b0\n    if ((file->flush_method & LINE_FLUSH) && file->outbuffer[file->bufferlen - 1] == '\\n') {\n        MYFflush(file);\n    }\n\n    return len;\n}\n\n\/\/ \u6d4b\u8bd5\u7528\u4f8b\nint main() {\n    MYFILE* fp = MYFopen(\"test.txt\", \"w\");\n    if (!fp) {\n        perror(\"MYFopen failed\");\n        return 1;\n    }\n\n    const char* msg1 = \"Hello, world!\\n\";\n    const char* msg2 = \"This is a test.\\n\";\n\n    MYFwrite(fp, msg1, strlen(msg1));\n    MYFwrite(fp, msg2, strlen(msg2));\n\n    MYFclose(fp);\n    return 0;\n}\n\u4e0b\u9762\u662f\u8fd0\u884c\u7ed3\u679c\uff1a\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"484\" height=\"32\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-5.png\" alt=\"\" class=\"wp-image-237\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-5.png 484w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-5-300x20.png 300w\" sizes=\"auto, (max-width: 484px) 100vw, 484px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"204\" height=\"36\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-6.png\" alt=\"\" class=\"wp-image-238\"\/><\/figure>\n\n\n\n<p>\u4eca\u5929\u7684\u66f4\u65b0\u5c31\u5230\u8fd9\u91cc\uff0c\u5982\u6709\u9519\u8bef\u6b22\u8fce\u8bc4\u8bba\u533a\u6307\u51fa\uff0c\u8c22\u8c22<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee3\u7801\u4ed3\u5e93\u5730\u5740\uff1ahttps:\/\/gitee.com\/tgwTTT\/linux-learning-dai\/tree&#8230;<\/p>\n","protected":false},"author":1,"featured_media":233,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-231","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=231"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/231\/revisions"}],"predecessor-version":[{"id":856,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/231\/revisions\/856"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/media\/233"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}