文件描述符、函数open和openat
生活随笔
收集整理的這篇文章主要介紹了
文件描述符、函数open和openat
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文件描述符
- pcb:結(jié)構(gòu)體
- 一個進程有一個文件描述符:1024
- 文件描述符:尋找磁盤文件
函數(shù)open和openat
函數(shù)原型:
include<sys/stst.h> #include<fcntl.h>int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode);int openat(int fd, const char *pathname, int flags); int openat(int fd, const char *pathname, int flags, mode_t mode);函數(shù)的返回值:若成功,返回文件描述符; 若出錯,返回-1flag參數(shù):必選項
| O_RDONLY | 只讀打開 |
| O_WRONLY | 只寫打開 |
| O_RDWR | 讀、寫打開 |
flag參數(shù):可選項
| O_APPEND | 每次寫時追加到文件的末尾 |
| O_CREAT | 若此文件不存在則創(chuàng)建它 |
| O_EXCL | 創(chuàng)建文件時檢測文件是否存在,必須與O_CREAT一起使用 |
| O_TRUNC | 如果文件存在,而且只寫或讀寫打開成功打開,則將其長度階段為0 |
mode參數(shù):mode &~ umask
?
測試代碼:
#include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<unistd.h> #include<stdio.h>int main(int argc, const char *argv[]) {int fd = open("hello", O_RDWR | O_CREAT, 0777);if(fd == -1) {printf("打開失敗\n");}close(fd);return 0; }輸出結(jié)果:
?
總結(jié)
以上是生活随笔為你收集整理的文件描述符、函数open和openat的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哈利波特魔法觉醒决斗录像怎么保存
- 下一篇: 成都欢乐谷半票多少钱