閱讀415 返回首頁    go 技術社區[雲棲]


linux c 文件打開並創建代碼分析

[root@luozhonghua 03]# cat ex03-open-03.c/*文件ex03-open-03.c,O_CREAT和O_EXCL的使用*/#include #include #include #include int main(void){ int fd = -1; char filename[] = "test.txt"; /*打開文件,如果文件不存在,則報錯*/ fd = open(filename,O_RDWR|O_CREAT|O_EXCL,S_IRWXU); /*O_RDWR讀寫模式,O_CREAT創建模式,O_EXCL存在與否模式,S_IRWXU讀寫權限,注意fd返回值0輸入,1輸出,2錯誤*/ if(-1 == fd){ /*文件已經存在*/ printf("File %s exist!,reopen it",filename); /*重新打開*/ fd = open(filename,O_RDWR); printf("fd:%d\n",fd); } else { /*文件不存在,創建並打開*/ printf("Open file %s success,fd:%d\n",filename,fd); } return 0;}

最後更新:2017-04-03 05:39:41

  上一篇:go poj 2081 Recaman's Sequence【hash】
  下一篇:go 高質量的HTML