{"id":592,"date":"2010-06-09T10:42:33","date_gmt":"2010-06-09T16:42:33","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=592"},"modified":"2012-06-04T20:50:23","modified_gmt":"2012-06-04T12:50:23","slug":"592","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/592","title":{"rendered":"create()\u548copen()"},"content":{"rendered":"<p>creat()\u548copen()\u90fd\u53ef\u4ee5\u521b\u5efa\u6587\u4ef6\uff0c\u4f46\u662f\u7a0d\u6709\u533a\u522b\u3002\u5982\u679c\u6309\u7167\u5982\u4e0b\u65b9\u5f0f\u8bbe\u7f6e\u53c2\u6570\u5219\u4e24\u8005\u662f\u7b49\u4ef7\u7684\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nopen(const char* pathname, (O_CREAT|O_WRONLY|O_TRUNC),mode_t mode);\r\ncreat(const char* pathname,mode_t mode);<\/pre>\n<p>\u82e5\u8981\u521b\u5efa\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u6309\u7167mode\u65b9\u5f0f\u521b\u5efa\u65b0\u6587\u4ef6\u3002\u5f53\u8981\u521b\u5efa\u7684\u6587\u4ef6\u5df2\u7ecf\u5b58\u5728\u5230\u65f6\u5019\uff0c\u8986\u76d6\u539f\u6709\u6587\u4ef6\u3002\u4e8b\u5b9e\u4e0a\u662f\u56e0\u4e3aO_WRONLY|O_TRUNC\u53c2\u6570\u7684\u539f\u56e0\uff1a\u5f53\u6587\u4ef6\u5b58\u5728\u5e76\u4e14\u4ee5\u53ef\u5199\u65b9\u5f0f\u6253\u5f00\u7684\u65f6\u5019\uff0c\u539f\u6709\u6587\u4ef6\u5230\u957f\u5ea6\u5219\u6e050,\u5373\u4f1a\u6e05\u7a7a\u539f\u6709\u6587\u4ef6\uff0c\u4f46\u662f\u6587\u4ef6\u5c5e\u6027\u4e0d\u53d8\uff0c\u4e0e\u539f\u6709\u6587\u4ef6\u65b9\u5f0f\u76f8\u540c\uff0c\u5e76\u4e0d\u4f1a\u8bbe\u7f6e\u6210mode\u65b9\u5f0f\u3002<\/p>\n<p>\u5728\u4e0b\u9762\u7684\u6e90\u4ee3\u7801\u4e2d\uff0c\u4ee5open\u51fd\u6570\u65b9\u5f0f\u521b\u5efa\u6587\u4ef6\u65f6\uff0c\u7531\u4e8e\u8bbe\u7f6e\u62c9O_CREAT|O_EXCL\u53c2\u6570\uff0c\u5f53\u8981\u521b\u5efa\u5230\u6587\u4ef6\u4e0d\u5b58\u5728\u65f6\uff0copen\u51fd\u6570\u521b\u5efa\u8fd9\u4e2a\u6587\u4ef6\uff0c\u5e76\u4e14\u6587\u4ef6\u5230\u5b58\u53d6\u6743\u9650\u4e3aS_IRUSR|S_IWUSR\u3002\u5f53\u6587\u4ef6\u5b58\u5728\u65f6\u56e0\u4e3a\u8bbe\u7f6e\u62c9O_EXCL\u5219\u51fa\u73b0\u9519\u8bef\u3002\u800c\u5f53\u4ee5creat()\u65b9\u5f0f\u521b\u5efa\u6587\u4ef6\u65f6\uff0c\u5982\u672c\u6587\u5f00\u5934\u6240\u8ff0\uff0c\u65e0\u8bba\u662f\u5426\u5b58\u5728\u5c06\u8981\u521b\u5efa\u7684\u90a3\u4e2a\u6587\u4ef6\uff0c\u90fd\u4e0d\u4f1a\u51fa\u73b0\u9519\u8bef\u3002<\/p>\n<p>\u4e0b\u9762\u4f5c\u6d4b\u8bd5\u3002<\/p>\n<p>\u9996\u5148\u6ce8\u610f\uff0c\u5728\u4e0b\u9762\u7684\u6e90\u4ee3\u7801\u4e2dopen()\u51fd\u6570\u4e2d\u7684\u6587\u4ef6\u6743\u9650s\u8bbe\u7f6e\u4e3a\uff1aS_IRUSR|S_IWUSR\uff08\u6587\u4ef6\u6240\u6709\u7740\u53ef\u8bfb\u53ef\u5199\uff09\u3002creat()\u8bbe\u7f6e\u7684\u6587\u4ef6\u6743\u9650\u4e3a\uff1aS_IRWXU\uff08\u6587\u4ef6\u6240\u6709\u8005\u53ef\u8bfb\u53ef\u5199\u53ef\u6267\u884c\uff09\u3002<\/p>\n<p>\u7b2c\u4e00\u6b21\u8fd0\u884c\u672c\u7a0b\u5e8f\uff0c\u9009\u62e9\u4f7f\u7528open()\u521b\u5efaexample_62.c\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nplease select a way of creating files:\r\n1.creat 2.open\r\n2\r\ncreate file success\r\nedsionte@edsionte-laptop:~\/code$ ls -l\r\n\u603b\u7528\u91cf 48\r\n-rw------- 1 edsionte edsionte    0 2010-06-16 12:38 example_62.c<\/pre>\n<p>\u7b2c\u4e8c\u6b21\u8fd0\u884c\u672c\u7a0b\u5e8f\uff0c\u4ee5creat()\u521b\u5efa\u6587\u4ef6\uff0c\u4f9d\u7136\u4f1a\u8fd0\u884c\u6210\u529f\uff0c\u4f46\u662f\u6b64\u65f6\u7684example_62.c\u7684\u5b58\u53d6\u6743\u9650\u662f\u5426\u662fcreat()\u4e2d\u8bbe\u7f6e\u7684S_IRWXU\u5462\uff1f<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nedsionte@edsionte-laptop:~\/code$ .\/my_create\r\nplease select a way of creating files:\r\n1.creat 2.open\r\n1\r\ncreate file success\r\nedsionte@edsionte-laptop:~\/code$ ls -l\r\n\u603b\u7528\u91cf 48\r\n-rw------- 1 edsionte edsionte    0 2010-06-16 12:40 example_62.c<\/pre>\n<p>\u6b63\u5982\u4e0a\u9762\u6240\u8ff0\u7684\uff0ccreat()\u521b\u5efa\u5df2\u5b58\u5728\u7684\u6587\u4ef6\uff0c\u5e76\u4e0d\u4f1a\u66f4\u6539\u539f\u6587\u4ef6\u7684\u5c5e\u6027\u3002<\/p>\n<p>\u6e90\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &quot;stdio.h&quot;\r\n#include &quot;sys\/types.h&quot;\r\n#include &quot;sys\/stat.h&quot;\r\n#include &quot;fcntl.h&quot;\r\n#include &quot;unistd.h&quot;\r\n#include &quot;error.h&quot;\r\n\r\nint main()\r\n{\r\n\tint fd,n;\r\n\tprintf(&quot;please select a way of creating files:\\n&quot;);\r\n\tprintf(&quot;1.creat 2.open\\n&quot;);\r\n\tscanf(&quot;%d&quot;,&amp;amp;n);\r\n\tif(n==1)\r\n\t{\r\n\t\tif((fd=creat(&quot;example_62.c&quot;,S_IRWXU))==-1)\r\n\t\t{\r\n\t\t\tperror(&quot;open&quot;);\r\n\t\t\texit(1);\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tprintf(&quot;create file success\\n&quot;);\r\n\t\t}\r\n\t}\r\n\telse if(n==2)\r\n\t{\r\n\t\tif((fd=open(&quot;example_62.c&quot;,O_CREAT|O_EXCL,S_IRUSR|S_IWUSR))==-1)\r\n\t\t{\r\n\t\t\tperror(&quot;open&quot;);\r\n\t\t\texit(1);\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tprintf(&quot;create file success\\n&quot;);\r\n\t\t}\r\n\t}\r\n\telse\r\n\t{\r\n\t\tprintf(&quot;your choice may be wrong\\n&quot;);\r\n\t}\r\n\tclose(fd);\r\n\treturn 0;\r\n}<\/pre>\n<p>create()\u548copen()\u662f\u6587\u4ef6\u64cd\u4f5c\u4e2d\u6700\u57fa\u672c\u7684\u51fd\u6570\uff0c\u5927\u5bb6\u5e94\u8be5\u5728\u5b9e\u8df5\u4e2d\u4e0d\u65ad\u603b\u7ed3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>creat()\u548copen()\u90fd\u53ef\u4ee5\u521b\u5efa\u6587\u4ef6\uff0c\u4f46\u662f\u7a0d\u6709\u533a\u522b\u3002\u5982\u679c\u6309\u7167\u5982\u4e0b\u65b9\u5f0f\u8bbe\u7f6e\u53c2\u6570\u5219\u4e24\u8005\u662f\u7b49\u4ef7\u7684\uff1a open( [&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-592","post","type-post","status-publish","format-standard","hentry","category-linuxc","tag-c","tag-linux","tag-107"],"views":6537,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/592","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=592"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/592\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=592"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}