{"id":289837,"date":"2024-02-05T05:10:48","date_gmt":"2024-02-04T21:10:48","guid":{"rendered":"https:\/\/lrxjmw.cn\/lwjczdkdqybd.html"},"modified":"2024-02-05T05:10:48","modified_gmt":"2024-02-04T21:10:48","slug":"lwjczdkdqybd","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/lwjczdkdqybd.html","title":{"rendered":"Linux\u6587\u4ef6\u64cd\u4f5c\uff1a\u6253\u5f00\u3001\u8bfb\u53d6\uff0c\u4e00\u6b65\u5230\u4f4d"},"content":{"rendered":"
\u611f\u8c22\u60a8\u5173\u5fc3Linux<\/a>\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u3002\u6b64\u4e3aLinux\u7cfb\u7edf<\/a>\u57fa\u7840\u529f\u80fd\u4e4b\u4e00\uff0c\u901a\u8fc7\u5185\u5efa\u7684\u5b9e\u7528\u5de5\u5177\u5305\u6765\u7b80\u5316\u590d\u6742\u7684\u6587\u4ef6\u64cd\u4f5c\u6d41\u7a0b\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5171\u540c\u63a2\u5bfb\u5176\u4e2d\u5965\u79d8\u3002<\/p>\n 1.\u6253\u5f00\u6587\u4ef6\uff1a<\/b><\/p>\n \u5728\u60a8\u9a7e\u9a6dLinux\u64cd\u4f5c\u7cfb\u7edf\u6765\u7ba1\u7406\u6587\u4ef6\u65f6\uff0c\u5de7\u5999\u8fd0\u7528open()\u51fd\u6570\uff0c\u4fbf\u53ef\u8f7b\u677e\u5b8c\u6210\u5404\u79cd\u6587\u4ef6\u7684\u5f00\u542f\u4efb\u52a1\u3002\u4f9d\u6b64\u89c4\u5f8b\uff0c\u8be5\u51fd\u6570\u9700\u8981\u6839\u636e\u4e24\u4e2a\u4e3b\u8981\u53c2\u6570\uff0c\u5373\u6240\u9700\u6253\u5f00\u7684\u6587\u4ef6\u540d\u53ca\u5bf9\u5176\u7684\u6253\u5f00\u6a21\u5f0f\uff0c\u6765\u6267\u884c\u76f8\u5173\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\"example.txt\"\u6587\u4ef6\uff0c\u60a8\u4e0d\u59a8\u8bd5\u8bd5\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n ```<\/p>\n \u5bf9\"example.txt\"\u8fdb\u884c\u53ea\u8bfb\u64cd\u4f5c\uff0c\u53ef\u4f7f\u7528\u5982\u4e0b\u8bed\u53e5\uff1a<\/p>\n int file;<\/p>\n \u80fd\u8bf7\u60a8\u501f\u9605\u4e00\u4e0b\u300cexample.txt\u300d\u6587\u4ef6\u5417\uff1f\u6211\u4eec\u9700\u8981\u4ee5O_RDONLY\u6a21\u5f0f\u8fdb\u884c\u8bfb\u53d6\u64cd\u4f5c\u3002<\/p>\n \u5c0a\u656c\u7684\u8bfb\u8005\u4eeclinux windows\uff0c\u6211\u4eec\u5f88\u9ad8\u5174\u80fd\u6210\u529f\u8bbf\u95ee\u5230\u8fd9\u4e00\u53ea\u8bfb\u6587\u4ef6\uff0c\u5e76\u4e3a\u5176\u53d6\u540d\u4e3a'file'\u3002<\/p>\n 2.\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff1a<\/b><\/p>\n \u5f53\u60a8\u6210\u529f\u5f00\u542f\u6587\u4ef6\u65f6\uff0c\u4e0d\u59a8\u5c1d\u8bd5\u7528`read()`\u51fd\u6570\u8f7b\u677e\u83b7\u53d6\u5176\u4e2d\u6570\u636e\u3002\u6b64\u529f\u80fd\u53ef\u63a5\u53d7\u4e09\u9879\u53c2\u6570\uff1a\u6587\u4ef6\u6807\u8bc6\u7b26\u3001\u5b58\u53d6\u6570\u636e\u7684\u7f13\u5b58\u533a\u57df\u5730\u5740\u53ca\u671f\u95f4\u53ef\u8bfb\u53d6\u5b57\u8282\u6570\u91cf\uff0c\u8bf7\u770b\u4ee5\u4e0b\u793a\u4f8b\uff1a<\/p>\n char buffer[11];<\/p>\n \u60a8\u660e\u767d\u6211\u7684\u610f\u601d\u5417\uff1f\u6ca1\u9519\uff0cbyteRead\u6307\u9488\u5df2\u7ecf\u88ab\u8bbe\u7f6e\u4e3aread\u51fd\u6570\u4ece\u4e2d\u8bfb\u53d6\u6587\u6863\u5185\u5bb9\u6240\u9700\u7684\u5b57\u8282\u6570\u4e86\u3002\u4e0d\u8fc7\uff0cuBuffer\u7684\u521d\u59cb\u5927\u5c0f\u8a2d\u5b9a\u5374\u662f10\u4e2a\u5b57\u8282\u54e6\u3002<\/p>\n buffer[bytesRead]='';<\/p>\n \u4eb2\u7231\u7684\u7528\u6237linux \u6587\u4ef6\u64cd\u4f5c\u51fd\u6570<\/strong>\uff0c\u611f\u6fc0\u4e4b\u81f3\u5730\u544a\u8bc9\u60a8\uff0c\u5df2\u987a\u5229\u83b7\u53d6\u4e86\u5982\u4e0b\u4fe1\u606f\uff1a%s\u3002\u611f\u8c22\u60a8\u7684\u652f\u6301\u548c\u914d\u5408\uff01<\/p>\n <\/p>\n \u8fd9\u6837\u6211\u4eec\u5c31\u6210\u529f\u5730\u8bfb\u53d6\u4e8610\u4e2a\u5b57\u8282\u5e76\u6253\u5370\u51fa\u6765\u4e86\u3002<\/p>\n 3.\u5199\u5165\u6587\u4ef6\uff1a<\/b><\/p>\n \u9664\u4e86\u8bfb\u53d6\u73b0\u6709\u6587\u6863\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u5229\u7528write()\u51fd\u6570\u5728\u5df2\u6709\u7684\u6587\u4ef6\u4e2d\u52a0\u65b0\u5185\u5bb9\u54e6\uff01\u8fd9\u4e2a\u51fd\u6570\u9700\u8981\u4e09\u4e2a\u57fa\u672c\u8f93\u5165\uff1a\u6587\u4ef6\u6807\u8bc6\u7b26\uff0c\u63a5\u7740\u662f\u5b58\u50a8\u5c06\u8981\u5199\u5165\u7684\u4fe1\u606f\u7684\u7f13\u51b2\u533a\u4f4d\u7f6e\uff0c\u6700\u540e\u662f\u60a8\u60f3\u8981\u5199\u5165\u591a\u5c11\u5b57\u8282\u7684\u5177\u4f53\u6570\u636e\u3002\u4e0b\u9762\u8fd9\u884c\u7b80\u6d01\u660e\u4e86\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u4f1a\u4ee5\u6700\u6613\u4e8e\u7406\u89e3\u7684\u65b9\u5f0f\u6765\u6f14\u793a\u5982\u4f55\u4e3a\u5df2\u6253\u5f00\u7684\u6587\u4ef6\u5185\u63d2\u77ed\u5c0f\u7684\u6587\u672c\u63cf\u8ff0\uff1a<\/p>\n \"\u4f60\u597d\uff0cLinux\uff01\"\u88ab\u5b9a\u4e49\u4e3a\u5b57\u7b26\u578b\u53d8\u91cftext\u3002<\/p>\n \u60a8\u597d\uff01\u5f88\u9ad8\u5174\u60a8\u80fd\u53c2\u52a0\uff0c\u63a5\u4e0b\u6765\u8bf7\u6839\u636e\u63d0\u793a\u586b\u5199\u6587\u672c\u53d8\u91cf\u53ca\u5176\u5b57\u7b26\u6570\u91cf\u5b8c\u6210\u6570\u636e\u5199\u5165\u6b65\u9aa4\uff1a\u53ea\u9700\u5c06\"bytes Written = write (file, text, strlen(text))\uff1b\"\u8fd9\u90e8\u5206\u66ff\u6362\u4e3a\u60a8\u6240\u9700\u7684\u5b57\u7b26\u4fe1\u606f\u5373\u53ef\u3002<\/p>\n \u6211\u4eec\u975e\u5e38\u8363\u5e78\u5730\u901a\u77e5\u60a8\uff0c\u5df2\u7ecf\u6210\u529f\u5b8c\u6210\u4e86\u5bf9%d\u5b57\u8282\u7684\u5199\u5165\u3002<\/p>\n \u8fd9\u6837\u6211\u4eec\u5c31\u6210\u529f\u5730\u5411\u6587\u4ef6\u4e2d\u5199\u5165\u4e86\u4e00\u6bb5\u6587\u672c\u3002<\/p>\n <\/p>\n 4.\u5173\u95ed\u6587\u4ef6\uff1a<\/b><\/p>\n \u5728\u60a8\u5b8c\u6210\u6240\u6709\u64cd\u4f5c\u540e\uff0c\u522b\u5fd8\u4e86\u59a5\u5584\u4fdd\u5b58\u54e6~\u8fd9\u91cc\u63d0\u4f9b\u4e00\u4e2a\u9ad8\u6548\u4e14\u4f18\u96c5\u7684Python\u89e3\u51b3\u65b9\u6848\u2014\u2014\u5229\u7528close()\u51fd\u6570\u3002\u60a8\u53ea\u8981\u5c06\u5f85\u5173\u95ed\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4f20\u5165\uff0c\u5c31\u80fd\u5706\u6ee1\u5b8c\u6210\u6b64\u9879\u4efb\u52a1\u5566\uff5e\u6bd4\u5982\u8fd9\u6837\u505a\uff1a<\/p>\n close(file);<\/p>\n \u8fd9\u6837\u6211\u4eec\u5c31\u6210\u529f\u5730\u5173\u95ed\u4e86\u6587\u4ef6\u3002<\/p>\n 5.\u521b\u5efa\u65b0\u6587\u4ef6\uff1a<\/b><\/p>\n \u6211\u4eec\u901a\u8fc7\u4f7f\u7528'create()'\u51fd\u6570\u8f7b\u677e\u5730\u521b\u5efa\u65b0\u7684\u6587\u6863\u3002\u8be5\u51fd\u6570\u9700\u8f93\u5165\u4e24\u90e8\u5206\u4fe1\u606f\uff1a\u6587\u4ef6\u540d\u4e0e\u6743\u9650\u8bbe\u5b9a\u3002\u5982\u56fe\u6240\u793a\uff0c\u5c06\u4e3a'newfile.txt'\u6587\u6863\u8d4b\u4e88\u9002\u5f53\u7684\u6743\u9650\u3002<\/p>\n \u53ea\u9700\u7b80\u5355\u4f7f\u7528\u201ccreate\u201d\u529f\u80fd\uff0c\u5373\u53ef\u4fbf\u6377\u5730\u751f\u6210\u65b0\u6587\u4ef6\uff0c\u4f8b\u5982\u5c06\"newfile.txt\"\u690d\u5165\u6307\u5b9a\u7684\u76ee\u5f55\u540e\u8d4b\u4e88\u5176\u6743\u9650\u4f4d 0644\uff0c\u65b9\u4fbf\u5feb\u6377\u3002<\/p>\n \u8fd9\u6837\u6211\u4eec\u5c31\u6210\u529f\u5730\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u3002<\/p>\n 6.\u5220\u9664\u6587\u4ef6\uff1a<\/b><\/p>\n \u5728\u6e05\u7406\u65e0\u7528\u6587\u4ef6\u65f6\uff0c\u4e0d\u59a8\u5584\u7528unlink()\u51fd\u6570\uff0c\u53ea\u9700\u63d0\u4f9b\u9700\u8981\u6e05\u9664\u7684\u6587\u4ef6\u540d\u8fd9\u4e00\u4e2a\u53c2\u6570\u5373\u53ef\uff0c\u6bd4\u5982\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u53ef\u987a\u5229\u6d88\u9664\u540d\u4e3a\"oldfile.txt\"\u7684\u6587\u6863\uff1a<\/p>\n unlink(\"oldfile.txt\");<\/p>\n \u8fd9\u6837\u6211\u4eec\u5c31\u6210\u529f\u5730\u5220\u9664\u4e86\u4e00\u4e2a\u6587\u4ef6\u3002<\/p>\n 7.\u91cd\u547d\u540d\u6587\u4ef6\uff1a<\/b><\/p>\n \u53ea\u9700\u8f7b\u8f7b\u4e00\u70b9\uff0c\u5373\u53ef\u5229\u7528rename()\u51fd\u6570\u8f7b\u677e\u66f4\u6539\u6587\u4ef6\u540d\u3002\u8be5\u51fd\u6570\u4ec5\u9700\u63d0\u4f9b\u539f\u6587\u4ef6\u540d\u4e0e\u65b0\u6587\u4ef6\u540d\u4f5c\u4e3a\u53c2\u6570\uff0c\u4fbf\u53ef\u5b9e\u73b0\u6587\u4ef6\u540d\u7684\u65e0\u7f1d\u66ff\u6362\u3002\u5982\u9700\u5c06\"oldfile.txt\"\u66f4\u6539\u4e3a\"newfile.txt\"\uff0c\u8bf7\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n <\/p>\n \u8bf7\u628a\u539f\u6765\u7684\u6587\u6863\"oldfile.txt\"\u66f4\u65b0\u6210\"newfile.txt\"\u7684\u65b0\u540d\u5b57\uff0c\u8c22\u8c22\uff01<\/p>\n \u8fd9\u6837\u6211\u4eec\u5c31\u6210\u529f\u5730\u5c06\u4e00\u4e2a\u6587\u4ef6\u91cd\u547d\u540d\u4e86\u3002<\/p>\n 8.\u590d\u5236\u6587\u4ef6\uff1a<\/b><\/p>\n