一、open函數
1、函數原型:int open(const char *path,int oflag,.../* mode_t mode */);
2、頭文件:#include <fcntl.h>
3、參數說明:
(1)path:要打開或創建文件的名字;
(2)oflag:用下列一個或多個常量進行“或”運算構成oflag參數;
a、文件讀寫模式常量:
O_RDONLY:文件以只讀方式打開;
O_WRONLY:文件以只寫模式打開;
O_RDWR:文件以可讀可寫模式打開。
b、其他常用常量:
O_APPEND:追加模式打開,即當給已有文件進行寫數據操作時,新數據會追加到原文件的末尾;
O_CREAT:若要操作的文件不存在,則會創建此文件,使用時需要同時指定第三個參數權限位;
O_EXCL:檢測文件是否存在,若同時指定了O_CREAT,且文件存在,則會報錯,若文件不存在,則創建此文件;
O_TRUNC:若文件存在,則在打開的同時會清空文件內容;
O_NOCTTY:若path引用的是終端設備,則不該將設備分配作為此進程的控制終端;
O_NONBLOCK:若path引用的是一個FIFO/一個塊特殊文件/一個字符特殊文件,則此選項為文件的本次打開操作和后續的I/O操作設置為非阻塞模式。
c、同步輸入輸出:(目前沒用到過,還不太清楚使用場景)
O_SYNC:使每次write等待物理I/O操作完成,包括由該write操作引起的文件屬性更新所需的I/O;
O_DSYNC:使每次write要等待物理I/O操作完成,但若該寫操作並不影響讀取剛寫入的數據,則不需要等待文件屬性被更新;
O_RSYNC:使每一個以文件描述符作為參數進行的read操作等待,直至所有對文件同一部分掛起的寫操作都完成。
4、示例:
1 #include <stdio.h> 2 #include <fcntl.h> 3 #include <unistd.h> 4 5 int main(int argc,char *argv[]) 6 { 7 int fd = open("test.txt",O_RDWR|O_CREAT|O_TRUNC,0777); 8 if (fd < 0) 9 perror("open"); 10 else 11 printf("fd = %d\n",fd); 12 13 close(fd); 14 15 return 0; 16 }
注意:open和close需要成對存在!
二、openat函數:
1、函數原型:int openat(int fd,const char *path,int oflag,.../* mode_t mode */);
2、頭文件:#include <fcntl.h>
3、參數說明:
(1)fd:
相對於open函數,此函數多了一個fd參數,異同點如下:
a、若path指定的是絕對路徑,fd參數被忽略,openat函數相當於open函數;
b、若path指定的是相對路徑,fd參數指出了相對路徑名在文件系統中的開始地址,fd參數是通過打開相對路徑名所在的目錄來獲取;
c、path指定了相對路徑名,fd參數具有特殊值AT_FDCWD.在此情況下,路徑名在當前工作目錄中獲取,openat函數在操作上與open類似。
(2)其余參數與open一致,在此不重復。
4、使用示例:主要考慮fd的獲取,打開目錄一般是DIR *dirfp = opendir("./"),需要處理,考慮以下兩種方式;
(1)利用 dirfd()函數將opendir返回的目錄描述符轉為int類型的文件描述符;
函數原型:int dirfd(DIR *dirp);
示例:
1 #include <sys/types.h> 2 #include <sys/stat.h> 3 #include <fcntl.h> 4 #include <dirent.h> 5 #include <stdio.h> 6 #include <unistd.h> 7 8 int main() 9 { 10 DIR *dir; 11 int dirfd2; 12 int fd; 13 int n; 14 15 dir = opendir("./"); 16 if(NULL == dir) 17 { 18 perror("open dir error"); 19 return -1; 20 } 21 dirfd2 = dirfd(dir); 22 if(-1 == dirfd2) 23 { 24 perror("dirfd error"); 25 return -1; 26 } 27 28 fd = openat(dirfd2,"output.log",O_CREAT|O_RDWR|O_TRUNC,0777); 29 if(-1 == fd) 30 { 31 perror("opeat error"); 32 return -1; 33 } 34 n = write(fd,"Hello world!\n",15); 35 36 close(fd); 37 closedir(dir); 38 39 return 0; 40 41 }
(2)直接用open打開一個目錄,其返回值作為openat的第一個參數:
示例:
1 #include <stdio.h> 2 #include <sys/stat.h> 3 #include <fcntl.h> 4 #include <stdlib.h> 5 #include <unistd.h> 6 7 void creat_at(char *dir_path, char *relative_path) 8 { 9 int dir_fd; 10 int fd; 11 12 dir_fd = open(dir_path, O_RDONLY); 13 if (dir_fd < 0) 14 { 15 perror("open"); 16 exit(EXIT_FAILURE); 17 } 18 19 fd = openat(dir_fd, relative_path, O_CREAT | O_TRUNC | O_RDWR, 0777); 20 if (fd < 0) 21 { 22 perror("openat"); 23 exit(EXIT_FAILURE); 24 } 25 26 write(fd, "HELLO\n", 6); 27 28 close(fd); 29 close(dir_fd); 30 } 31 32 int main() 33 { 34 creat_at("./", "log.txt"); 35 return 0; 36 }
