{"id":638,"date":"2010-06-17T20:47:56","date_gmt":"2010-06-18T02:47:56","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=638"},"modified":"2010-06-21T04:47:03","modified_gmt":"2010-06-21T10:47:03","slug":"linux%e4%b8%8bc%e7%bc%96%e7%a8%8b%e9%94%99%e8%af%af%e6%8d%95%e8%8e%b7%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/638","title":{"rendered":"linux\u4e0bC\u7f16\u7a0b\u9519\u8bef\u6355\u83b7\u51fd\u6570"},"content":{"rendered":"<p>\u672c\u6587\u4e2d\u7684\u9519\u8bef\u662f\u6307\u5728\u4ee3\u7801\u7f16\u8bd1\u5b8c\u5168\u6b63\u786e\u7a0b\u5e8f\u53ef\u8fd0\u884c\u7684\u60c5\u51b5\u4e0b\uff0c\u56e0\u4e3a\u6ca1\u6709\u6210\u529f\u8c03\u7528\u7a0b\u5e8f\u4e2d\u7684\u67d0\u4e9b\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\u800c\u4ea7\u751f\u7684\u9519\u8bef\u3002\u5f80\u5f80\u8fd9\u4e9b\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\u901a\u8fc7\u8fd4\u56de\u503c\uff08\u6bd4\u59821\uff0c0\uff0c\uff0d1\uff09\u6765\u8bf4\u660e\u5176\u662f\u5426\u8c03\u7528\u6210\u529f\uff0c\u800c\u7a0b\u5e8f\u5458\u9700\u8981\u77e5\u9053\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u56e0\u6b64\u81ea\u5efa\u9519\u8bef\u6355\u83b7\u51fd\u6570\u5f88\u6709\u5fc5\u8981\u3002<\/p>\n<p>(1)errno\u548cstrerror()<\/p>\n<p>errno\u5b83\u662f\u4e00\u4e2a\u6574\u5f62\u7684\u9519\u8bef\u4ee3\u7801\u3002\u5f53\u53d1\u751f\u9519\u8bef\u7684\u65f6\u5019\uff0c\u7cfb\u7edf\u81ea\u52a8\u5c06\u9519\u8bef\u4ee3\u7801\u8d4b\u7ed9errno\u3002\u4f7f\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\u53ef\u4ee5\u83b7\u5f97\u5177\u4f53\u7684\u9519\u8bef\u63cf\u8ff0\uff1a<\/p>\n<pre class=\"brush:\">void my_err(int error)\r\n{\r\n\tprintf(\"error:  %s with errno: %d\\n\",strerror(error),error);\r\n\texit(1);\r\n}\r\n\r\nint main()\r\n{\r\n\t        ..............\r\n\t\tmy_err(errno);\r\n\t\t..............\r\n\r\n}<\/pre>\n<p>\u5176\u4e2dchar *strerror(int errnum);\u662f\u901a\u8fc7errnum\u6765\u83b7\u53d6\u9519\u8bef\u63cf\u8ff0\uff0cerrnum\u5373\u6240\u4f20\u9012\u7684errno\u3002\u8be5\u51fd\u6570\u672b\u5c3e\u7684exit(1)\u4f7f\u5f97\u7a0b\u5e8f\u53d1\u751f\u9519\u8bef\u65f6\u9000\u51fa\u3002\u4f46\u5e94\u8be5\u5305\u542b\u5e93\u51fd\u6570stdlib.h\u3002<\/p>\n<p>\u4e0b\u9762\u8fdb\u884c\u6d4b\u8bd5\uff0c\u6d4b\u8bd5\u7a0b\u5e8f\uff08\u6e90\u4ee3\u7801\u5728\u672c\u6587\u672b\u5c3e\u3002\uff09\u4f7f\u7528open()\u51fd\u6570\u521b\u5efa\u6587\u4ef6\uff0c\u56e0\u4e3a\u8981\u521b\u5efa\u7684\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u800c\u4e14\u4f7f\u7528\u4e86O_EXCL\u53c2\u6570\uff0c\u56e0\u6b64open()\u4f1a\u4ea7\u751f\u9519\u8bef\u3002\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:\">edsionte@edsionte-laptop:~\/code$ .\/error\r\nerror:   File exists with errno: 17<\/pre>\n<p>\u8be5\u65b9\u6cd5\u53ef\u4ee5\u8be6\u7ec6\u663e\u793a\u9519\u8bef\u4fe1\u606f\u4ee5\u53ca\u9519\u8bef\u4ee3\u7801\u3002\u4f46\u4e0d\u80fd\u663e\u793a\u9519\u8bef\u51fa\u73b0\u7684\u884c\u6570\u3002<\/p>\n<p>(2)perror()<\/p>\n<p>\u5176\u51fd\u6570\u539f\u578b\u4e3a\uff1avoid perror(const char *s)\u3002s\u4e00\u822c\u662f\u51fd\u6570\u540d\u3002\u8be5\u51fd\u6570\u4f1a\u5148\u5c06\u51fd\u6570\u540d\u6253\u5370\u51fa\u6765\uff0c\u7136\u540e\u518d\u6253\u5370\u51fa\u9519\u8bef\u4fe1\u606f\u3002\u9519\u8bef\u4fe1\u606f\u4e0eerrno\u76f8\u5bf9\u5e94\u3002\u7b2c\u4e8c\u4e2a\u53c2\u6570__LINE__\u662f\u4e00\u4e2a\u5b8f\uff0c\u8868\u793a\u5f53\u524d\u7684\u884c\u6570\u3002\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush:\">void my_err2(const char* err_string,int line)\r\n{\r\n\tfprintf(stderr,\"error:  line:%d \",line);\r\n\tperror(err_string);\r\n        exit(1);\r\n}\r\n}\r\n\r\nint main()\r\n{\r\n\t        .................\r\n\t\tmy_err2(\"open\",__LINE__);\r\n                ................\r\n\r\n}<\/pre>\n<p>\u6d4b\u8bd5\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:\">edsionte@edsionte-laptop:~\/code$ .\/error\r\nerror:  line:29 open: File exists\r\n}<\/pre>\n<p>\u8be5\u65b9\u6cd5\u53ef\u4ee5\u663e\u793a\u9519\u8bef\u4fe1\u606f\u4ee5\u53ca\u9519\u8bef\u51fa\u73b0\u7684\u884c\u6570\u3002<\/p>\n<p>\u4ee5\u4e0a\u65b9\u6cd5\u662f\u5728\u300alinux C\u7f16\u7a0b\u300b\u4e2d\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u6211\u9002\u5f53\u7684\u4f5c\u4e86\u5c0f\u8c03\u6574\u3002\u73b0\u5728\u5c06\u8fd9\u4e24\u79cd\u65b9\u6cd5\u7ed3\u5408\u8d77\u6765\uff1a<\/p>\n<pre class=\"brush:\">void my_err3(const char* err_string,int line,int error)\r\n{\r\n\tprintf(\"error:  line:%d %s():%s with errno:%d\\n\",line,err_string,strerror(error),error);\r\n        exit(1);\r\n}\r\n\r\nint main()\r\n{\r\n\t        ................\r\n\t\tmy_err3(\"open\",__LINE__,errno);\r\n\t        ................\r\n\r\n}<\/pre>\n<p>\u6d4b\u8bd5\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:\">edsionte@edsionte-laptop:~\/code$ .\/error\r\nerror:  line:30 open():File exists with errno:17<\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u663e\u793a\u9519\u8bef\u4ee3\u7801\uff0c\u9519\u8bef\u63cf\u8ff0\uff0c\u9519\u8bef\u51fa\u73b0\u7684\u884c\u6570\u4ee5\u53ca\u51fa\u73b0\u9519\u8bef\u7684\u51fd\u6570\u3002\u5bf9\u4e8e\u548c\u6211\u4e00\u6837\u7684\u65b0\u624b\u6765\u8bf4\uff0c\u8fd9\u91cc\u7279\u522b\u8981\u6ce8\u610f\u7684\u662f\u5b8f<strong>__LINE__<\/strong>\u524d\u540e\u7684\u90a3\u4e2a\u6a2a\u7ebf\u662f\u4e24\u4e2a\u8fde\u7eed\u7684\u4e0b\u5212\u7ebf\uff0c\u800c\u4e0d\u662f<strong>_LINE_<\/strong>\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u9519\u8bef\u3002<br \/>\n\u6e90\u4ee3\u7801\u5982\u4e0b\uff1a<br \/>\n\u8bf4\u660e\uff1a\u672c\u7a0b\u5e8f\u53ea\u4f5c\u6d4b\u8bd5\u7528\uff0c\u4e3a\u4e86\u540c\u65f6\u663e\u793a\u4e09\u79cd\u9519\u8bef\u6355\u83b7\u51fd\u6570\u7684\u4fe1\u606f\uff0c\u56e0\u6b64\u5c4f\u853d\u4e86\u6bcf\u4e2a\u51fd\u6570\u7684exit(1)\u3002\u53e6\u5916\u672c\u6587\u5934\u6587\u4ef6\u51fd\u6570\u7528\u201c\u201d\u662f\u56e0\u4e3a\u663e\u793a\u95ee\u9898\uff0c\u6ca1\u6709\u4ec0\u4e48\u7279\u522b\u610f\u4e49\u3002<\/p>\n<pre class=\"brush:\">#include \"errno.h\"\r\n#include \"fcntl.h\"\r\n#include \"unistd.h\"\r\n#include \"stdlib.h\"\r\n#include \"stdio.h\r\n#include \"sys\/types.h\"\r\n#include \"sys\/stat.h\"\r\n#include \"string.h\"\r\n\r\nvoid my_err(int error)\r\n{\r\n\tprintf(\"error:  %s with errno: %d\\n\",strerror(error),error);\r\n\/\/\texit(1);\r\n}\r\nvoid my_err2(const char* err_string,int line)\r\n{\r\n\tfprintf(stderr,\"error:  line:%d \",line);\r\n\tperror(err_string);\r\n\/\/\texit(1);\r\n}\r\nvoid my_err3(const char* err_string,int line,int error)\r\n{\r\n\tprintf(\"error:  line:%d %s():%s with errno:%d\\n\",line,err_string,strerror(error),error);\r\n\/\/      exit(1);\r\n}\r\nint main()\r\n{\r\n\tint fd;\r\n\tif((fd=open(\"example_test.c\",O_CREAT|O_EXCL,S_IRUSR|S_IWUSR))==-1)\r\n\t{\r\n\t\tmy_err(errno);\r\n\t\tmy_err2(\"open\",__LINE__);\r\n\t\tmy_err3(\"open\",__LINE__,errno);\r\n\t}\r\n\tclose(fd);\r\n\treturn 0;\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4e2d\u7684\u9519\u8bef\u662f\u6307\u5728\u4ee3\u7801\u7f16\u8bd1\u5b8c\u5168\u6b63\u786e\u7a0b\u5e8f\u53ef\u8fd0\u884c\u7684\u60c5\u51b5\u4e0b\uff0c\u56e0\u4e3a\u6ca1\u6709\u6210\u529f\u8c03\u7528\u7a0b\u5e8f\u4e2d\u7684\u67d0\u4e9b\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\u800c\u4ea7\u751f\u7684\u9519\u8bef\u3002\u5f80 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[109,108,107],"class_list":["post-638","post","type-post","status-publish","format-standard","hentry","category-linuxc","tag-c","tag-linux","tag-107"],"views":10036,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=638"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/638\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=638"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}