日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux文件-access函数

發布時間:2025/3/15 linux 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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_fifo

file_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函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。