linux系統編程:open常用參數詳解,linux參數詳解

來源:互聯網
上載者:User

linux系統編程:open常用參數詳解,linux參數詳解

open用於開啟一個檔案,通過設定不同的flag,可以讓進程唯讀,唯寫,可讀/可寫等操作

一、對一個不存在或者存在的檔案(test.txt),進行寫入操作 1 /*================================================================ 2 * Copyright (C) 2018 . All rights reserved. 3 * 4 * 檔案名稱:cp.c 5 * 創 建 者:ghostwu(吳華) 6 * 建立日期:2018年01月10日 7 * 描 述:用open和write實現cp命令 8 * 9 ================================================================*/10 11 #include <stdio.h>12 #include <sys/types.h>13 #include <sys/stat.h>14 #include <fcntl.h>15 #include <errno.h>16 17 int main(int argc, char *argv[])18 {19 int fd, openflags;20 21 //openflags = O_WRONLY | O_CREAT;22 23 openflags = O_WRONLY;24 25 fd = open( "test.txt" , openflags );26 if( fd < 0 ) {27 printf( "%d\n", errno );28 perror( "write to file" );29 }30 return 0;31 }View Code

1)當test.txt不存在時, 檔案開啟失敗,會把作業系統的全域變數errno設定一個錯誤號碼, 這裡設定的是2,對於一個2,我們完全不知道是什麼錯誤,所以調用perror函數,他會把編號2解釋成可讀性的錯誤資訊,

那麼這裡被解讀成"No such file or directory",可以通過"grep "No such file or directory" /usr/include/*/*.h" 找到errno為2對應的標頭檔

2)如果test.txt檔案存在,不會報錯,開啟一個正常的檔案描述符,檔案原來的內容不會受影響

二、加入O_CREAT標誌 1 /*================================================================ 2 * Copyright (C) 2018 . All rights reserved. 3 * 4 * 檔案名稱:cp.c 5 * 創 建 者:ghostwu(吳華) 6 * 建立日期:2018年01月10日 7 * 描 述:用open和write實現cp命令 8 * 9 ================================================================*/10 11 #include <stdio.h>12 #include <sys/types.h>13 #include <sys/stat.h>14 #include <fcntl.h>15 #include <errno.h>16 17 int main(int argc, char *argv[])18 {19 int fd, openflags;20 21 //openflags = O_WRONLY | O_CREAT;22 23 openflags = O_WRONLY | O_CREAT;24 25 fd = open( "test.txt" , openflags );26 printf( "fd = %d\n", fd );27 if( fd < 0 ) {28 printf( "%d\n", errno );29 perror( "write to file" );30 }31 return 0;32 }View Code

1)如果test.txt不存在,那就建立test.txt,不會報錯

2)如果test.txt存在,test.txt原內容不會受到影響

三、加入O_TRUNC標誌
openflags = O_WRONLY | O_CREAT | O_TRUNC;

1)如果test.txt不存在,那就建立test.txt,不會報錯

2)如果test.txt存在,test.txt原內容會被清空

四、許可權位 1 /*================================================================ 2 * Copyright (C) 2018 . All rights reserved. 3 * 4 * 檔案名稱:cp.c 5 * 創 建 者:ghostwu(吳華) 6 * 建立日期:2018年01月10日 7 * 描 述:用open和write實現cp命令 8 * 9 ================================================================*/10 11 #include <stdio.h>12 #include <sys/types.h>13 #include <sys/stat.h>14 #include <fcntl.h>15 #include <errno.h>16 17 int main(int argc, char *argv[])18 {19 int fd, openflags;20 mode_t fileperms;21 22 openflags = O_WRONLY | O_CREAT | O_TRUNC;23 24 //rwxrw-r-x25 fileperms = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IXOTH;26 27 fd = open( "test.txt" , openflags, fileperms );28 printf( "fd = %d\n", fd );29 if( fd < 0 ) {30 printf( "%d\n", errno );31 perror( "write to file" );32 }33 return 0;34 }View Code 五、O_APPEND追加

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.