{"id":786,"date":"2024-11-07T08:10:44","date_gmt":"2024-11-07T00:10:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/786\/"},"modified":"2024-11-07T08:10:44","modified_gmt":"2024-11-07T00:10:44","slug":"c%e8%af%ad%e8%a8%80%e6%96%87%e4%bb%b6%e6%93%8d%e4%bd%9c%ef%bc%9a%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e6%96%87%e4%bb%b6%e7%b3%bb%e7%bb%9f%e8%b0%83%e7%94%a8%e9%94%99%e8%af%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/786\/","title":{"rendered":"C\u8bed\u8a00\u6587\u4ef6\u64cd\u4f5c\uff1a\u5982\u4f55\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef\uff1f"},"content":{"rendered":"<p>c \u8bed\u8a00\u4e2d\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef\u9700\u8981\u68c0\u67e5 errno \u5168\u5c40\u53d8\u91cf\uff0c\u4f7f\u7528 perror() \u51fd\u6570\u6253\u5370\u9519\u8bef\u63cf\u8ff0\uff0c\u5e76\u6839\u636e\u9519\u8bef\u91c7\u53d6\u9002\u5f53\u884c\u52a8\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u4ee3\u7801\u6f14\u793a\u5982\u4f55\u901a\u8fc7\u68c0\u67e5 errno \u548c\u4f7f\u7528 perror() \u6765\u5904\u7406\u6587\u4ef6\u6253\u5f00\u9519\u8bef\u3002\u6b64\u5916\uff0cc \u8bed\u8a00\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u51fd\u6570\uff0c\u4f8b\u5982 strerror()\u3001perror() \u548c set_errno()\uff0c\u7528\u4e8e\u5904\u7406\u5176\u4ed6\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1118\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172897254236806.jpg\" width=\"720\" height=\"288\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172897254236806.jpg 720w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172897254236806-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172897254236806-670x268.jpg 670w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" title=\"C\u8bed\u8a00\u6587\u4ef6\u64cd\u4f5c\uff1a\u5982\u4f55\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef\uff1f\u63d2\u56fe\" alt=\"C\u8bed\u8a00\u6587\u4ef6\u64cd\u4f5c\uff1a\u5982\u4f55\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>C \u8bed\u8a00\u6587\u4ef6\u64cd\u4f5c\uff1a\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef<\/strong><\/p>\n<p>\u5728 C \u8bed\u8a00\u4e2d\uff0c\u5bf9\u6587\u4ef6\u7cfb\u7edf\u8fdb\u884c\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\u3002\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u81f3\u5173\u91cd\u8981\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u53ef\u9760\u6027\u548c\u5065\u58ee\u6027\u3002<\/p>\n<p><strong>\u9519\u8bef\u5904\u7406\u673a\u5236<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201cC\u8bed\u8a00\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b<\/p>\n<p>C \u8bed\u8a00\u4f7f\u7528 errno \u5168\u5c40\u53d8\u91cf\u6765\u62a5\u544a\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef\u3002\u6b64\u53d8\u91cf\u5305\u542b\u4e00\u4e2a\u6574\u6570\uff0c\u8868\u793a\u53d1\u751f\u7684\u7279\u5b9a\u9519\u8bef\u3002<\/p>\n<p>\u4e3a\u4e86\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef\uff0c\u8bf7\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u5728\u6587\u4ef6\u64cd\u4f5c\u540e\u7acb\u5373\u68c0\u67e5 errno \u503c\u3002<\/li>\n<li>\u4f7f\u7528 perror() \u51fd\u6570\u6253\u5370\u9519\u8bef\u63cf\u8ff0\u3002<\/li>\n<li>\u6839\u636e\u9519\u8bef\u91c7\u53d6\u9002\u5f53\u7684\u884c\u52a8\uff0c\u4f8b\u5982\u6253\u5370\u9519\u8bef\u6d88\u606f\u3001\u7ec8\u6b62\u7a0b\u5e8f\u6216\u91cd\u8bd5\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u5982\u4f55\u5904\u7406\u6587\u4ef6\u6253\u5f00\u9519\u8bef\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\n#include &lt;errno.h&gt;\n\nint main() {\n  FILE *file;\n\n  \/\/ \u5c1d\u8bd5\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u8fd4\u56de NULL\n  file = fopen(\"file.txt\", \"r\");\n\n  if (file == NULL) {\n    \/\/ \u6587\u4ef6\u6253\u5f00\u5931\u8d25\uff0c\u68c0\u67e5\u9519\u8bef\u5e76\u6253\u5370\u63cf\u8ff0\n    perror(\"fopen\");\n    return 1;\n  }\n\n  \/\/ \u6210\u529f\u6253\u5f00\u6587\u4ef6\uff0c\u6267\u884c\u64cd\u4f5c\n\n  \/\/ ...\n\n  fclose(file);\n\n  return 0;\n}&lt;\/errno.h&gt;&lt;\/stdio.h&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u7a0b\u5e8f\u5c06\u6253\u5370\u4ee5\u4e0b\u9519\u8bef\u6d88\u606f\u5e76\u9000\u51fa\uff1a<\/p>\n<pre>fopen: No such file or directory<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef<\/strong><\/p>\n<p>\u9664\u4e86\u6587\u4ef6\u6253\u5f00\u9519\u8bef\uff0cC \u8bed\u8a00\u8fd8\u63d0\u4f9b\u4e86\u5904\u7406\u5176\u4ed6\u5e38\u89c1\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef\u7684\u51fd\u6570\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>strerror(errno)\uff1a\u83b7\u53d6\u4e0e errno \u5173\u8054\u7684\u9519\u8bef\u63cf\u8ff0\u5b57\u7b26\u4e32\u3002<\/li>\n<li>perror(prefix)\uff1a\u6253\u5370\u524d\u7f00\u6d88\u606f\uff0c\u540e\u8ddf\u7531 errno \u6307\u5b9a\u7684\u9519\u8bef\u63cf\u8ff0\u3002<\/li>\n<li>set_errno(errno)\uff1a\u8bbe\u7f6e errno \u503c\u4ee5\u6a21\u62df\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u6b63\u786e\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fC\u8bed\u8a00\u6587\u4ef6\u64cd\u4f5c\uff1a\u5982\u4f55\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>c \u8bed\u8a00\u4e2d\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef\u9700\u8981\u68c0\u67e5 errno \u5168\u5c40\u53d8\u91cf\uff0c\u4f7f\u7528 perror() \u51fd\u6570\u6253\u5370\u9519\u8bef\u63cf\u8ff0\uff0c\u5e76\u6839\u636e\u9519\u8bef\u91c7\u53d6\u9002\u5f53\u884c\u52a8\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u4ee3\u7801\u6f14\u793a\u5982\u4f55\u901a\u8fc7\u68c0\u67e5 errno \u548c\u4f7f\u7528 perror() \u6765\u5904\u7406\u6587\u4ef6\u6253\u5f00\u9519\u8bef\u3002\u6b64\u5916\uff0cc \u8bed\u8a00\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u51fd\u6570\uff0c\u4f8b\u5982 strerror()\u3001perror() \u548c set_errno()\uff0c\u7528\u4e8e\u5904\u7406\u5176\u4ed6\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef\u3002 C \u8bed\u8a00\u6587\u4ef6\u64cd\u4f5c\uff1a\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef \u5728 C \u8bed\u8a00\u4e2d\uff0c\u5bf9\u6587\u4ef6\u7cfb\u7edf\u8fdb\u884c\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\u3002\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u81f3\u5173\u91cd\u8981\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u53ef\u9760\u6027\u548c\u5065\u58ee\u6027\u3002 \u9519\u8bef\u5904\u7406\u673a\u5236 \u7acb\u5373\u5b66\u4e60\u201cC\u8bed\u8a00\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b C \u8bed\u8a00\u4f7f\u7528 errno \u5168\u5c40\u53d8\u91cf\u6765\u62a5\u544a\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef\u3002\u6b64\u53d8\u91cf\u5305\u542b\u4e00\u4e2a\u6574\u6570\uff0c\u8868\u793a\u53d1\u751f\u7684\u7279\u5b9a\u9519\u8bef\u3002 \u4e3a\u4e86\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8c03\u7528\u9519\u8bef\uff0c\u8bf7\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u5728\u6587\u4ef6\u64cd\u4f5c\u540e\u7acb\u5373\u68c0\u67e5 errno \u503c\u3002 \u4f7f\u7528 perror() \u51fd\u6570\u6253\u5370\u9519\u8bef\u63cf\u8ff0\u3002 \u6839\u636e\u9519\u8bef\u91c7\u53d6\u9002\u5f53\u7684\u884c\u52a8\uff0c\u4f8b\u5982\u6253\u5370\u9519\u8bef\u6d88\u606f\u3001\u7ec8\u6b62\u7a0b\u5e8f\u6216\u91cd\u8bd5\u64cd\u4f5c\u3002 \u5b9e\u6218\u6848\u4f8b \u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u5982\u4f55\u5904\u7406\u6587\u4ef6\u6253\u5f00\u9519\u8bef\uff1a #include &lt;stdio.h&gt; #include &lt;errno.h&gt; int main() { FILE *file; \/\/ \u5c1d\u8bd5\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u8fd4\u56de NULL file = fopen(&#8220;file.txt&#8221;, &#8220;r&#8221;); if (file == NULL) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-786","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=786"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/786\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}