linux文件-access函数
生活随笔
收集整理的這篇文章主要介紹了
linux文件-access函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?access函數:
access函數主要用于在使用文件之前獲取文件的屬性以免錯誤的使用文件的權限,造成文件讀寫過程中出錯;
#include <unistd.h>int access(const char *pathname, int mode); 成功返回0,出錯返回-1 功能:檢查是否可以對某文件進行某種操作 F_OK 值為0,判斷文件是否存在R_OK 值為4,判斷對文件是否有讀權限W_OK 值為2,判斷對文件是否有寫權限X_OK 值為1,判斷對文件是否有讀寫權限?堅持使用代碼說話,編寫access的函數并進行測試:
編寫函數file_access.c函數,并編譯運行進行測試測試結果如下:
andrew@andrew-Thurley:~/work/filedir$ ./a.out * 7852 can read a.out 7852 can write a.out 7852 can rexcute a.out 7852 can read bin 7852 can write bin 7852 can rexcute bin 7852 can read date.txt 7852 can write date.txt 7852 can not excute date.txt 7852 can read include 7852 can write include 7852 can rexcute include 7852 can read l_date 7852 can write l_date 7852 can not excute l_date 7852 can read obj 7852 can write obj 7852 can rexcute obj 7852 can read src 7852 can write src 7852 can rexcute src 7852 can read zieckey_fifo 7852 can write zieckey_fifo 7852 can rexcute zieckey_fifofile_access.c函數:
#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <string.h>int main(int argc, char *argv[]) {if(argc < 2){fprintf(stderr, "usage: %s files \n", argv[0]);exit(1);}int i;for(i = 1; i < argc; i++){if(access(argv[i], R_OK)){printf("%d can not read %s\n", getpid(),argv[i]);}else{printf("%d can read %s\n", getpid(), argv[i]);}if(access(argv[i], W_OK)){printf("%d can not write %s\n", getpid(),argv[i]);}else{printf("%d can write %s\n", getpid(), argv[i]);}if(access(argv[i], X_OK)){printf("%d can not excute %s\n", getpid(),argv[i]);}else{printf("%d can rexcute %s\n", getpid(), argv[i]);}}return 0; }?
#include <fcntl.h> /* Definition of AT_* constants */#include <unistd.h>int faccessat(int dirfd, const char *pathname, int mode, int flags);?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的linux文件-access函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: STM32寄存器与输入捕获
- 下一篇: 作者:张群(1988-),女,博士,中