C語(yǔ)言中access/_access函數(shù)的使用實(shí)例詳解
在Linux下,access函數(shù)的聲明在<unistd.h>文件中,聲明如下:
int access(const char *pathname, int mode);
access函數(shù)用來(lái)判斷指定的文件或目錄是否存在(F_OK),已存在的文件或目錄是否有可讀(R_OK)、可寫(xiě)(W_OK)、可執(zhí)行(X_OK)權(quán)限。F_OK、R_OK、W_OK、X_OK這四種方式通過(guò)access函數(shù)中的第二個(gè)參數(shù)mode指定。如果指定的方式有效,則此函數(shù)返回0,否則返回-1。
在Windows下沒(méi)有access函數(shù),但在<io.h>文件中有_access函數(shù),聲明如下:
int _access(const char* _Filename, int _AccessMode);
windows下的函數(shù)_access與linux下的access函數(shù)功能類似,用來(lái)判斷指定的文件或目錄是否僅存在(00),已存在的文件或目錄是否有僅讀(04)、僅寫(xiě)(02)、既可讀又可寫(xiě)(06)權(quán)限。這四種方式通過(guò)_access函數(shù)中的第二個(gè)參數(shù)mode指定,如果mode傳入的值不是0或2或4或6,調(diào)用此函數(shù)則會(huì)crash。如果指定的方式有效,則此函數(shù)返回0,否則返回-1。
以下是測(cè)試代碼(access.cpp):
#include "access.hpp"
#include <iostream>
#include <vector>
#include <string>
#ifdef _MSC_VER
#include <io.h>
#else
#include <unistd.h>
#endif
namespace access_ {
int test_access_1()
{
#ifdef _MSC_VER
const std::string path{ "E:/GitCode/Messy_Test/" };
const std::vector<const std::string> names {"testdata", ".gitignore", "src", "invalid"};
for (auto& name : names) {
const std::string tmp = path + name;
fprintf(stdout, "file or directory name: \"%s\": ", name.c_str());
if (_access(tmp.c_str(), 0) == 0) fprintf(stdout, "exist, ");
else fprintf(stdout, "not exist, ");
if (_access(tmp.c_str(), 4) == 0) fprintf(stdout, "only has read premission, ");
else fprintf(stdout, "does not have read premission, ");
if (_access(tmp.c_str(), 2) == 0) fprintf(stdout, "only has write premission, ");
else fprintf(stdout, "does not have write premission, ");
if (_access(tmp.c_str(), 6) == 0) fprintf(stdout, "has both read and write premission\n");
else fprintf(stdout, "has neither read nor write premission\n");
}
#else
const std::vector<const char*> names {"testdata", "CMakeLists.txt", "build.sh", "invalid"};
for (auto name : names) {
fprintf(stdout, "file or directory name: \"%s\": ", name);
if (access(name, F_OK) == 0) fprintf(stdout, "exist, ");
else fprintf(stdout, "not exist, ", name);
if (access(name, R_OK) == 0) fprintf(stdout, "has read premission, ");
else fprintf(stdout, "does not have read premission, ");
if (access(name, W_OK) == 0) fprintf(stdout, "has write premission, ");
else fprintf(stdout, "does not have write premission, ");
if (access(name, X_OK) == 0) fprintf(stdout, "has execute premission\n");
else fprintf(stdout, "does not have execute premission\n");
}
#endif
return 0;
}
} // namespace access_
在Linux下的執(zhí)行結(jié)果如下:

GitHub:https://github.com//fengbingchun/Messy_Test
總結(jié)
以上所述是小編給大家介紹的C語(yǔ)言中access/_access函數(shù)的使用實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
C++11關(guān)于auto關(guān)鍵字的使用示例
今天小編就為大家分享一篇關(guān)于C++11關(guān)于auto關(guān)鍵字的使用示例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
C語(yǔ)言中一些將字符串轉(zhuǎn)換為數(shù)字的函數(shù)小結(jié)
這篇文章主要介紹了C語(yǔ)言中一些將字符串轉(zhuǎn)換為數(shù)字的函數(shù)小結(jié),分別為atoi()函數(shù)和atol()函數(shù)以及atof()函數(shù),需要的朋友可以參考下2015-08-08
C語(yǔ)言實(shí)現(xiàn)切片數(shù)組的示例詳解
由于c語(yǔ)言沒(méi)有集合類的標(biāo)準(zhǔn)庫(kù),需要用時(shí)只能自己實(shí)現(xiàn),所以本文參考了go語(yǔ)言的slice,找到了一種非常簡(jiǎn)化的動(dòng)態(tài)數(shù)組接口,下面我們就來(lái)看看如何在C語(yǔ)言中實(shí)現(xiàn)切片吧2024-03-03
C++中繼承與多態(tài)的基礎(chǔ)虛函數(shù)類詳解
這篇文章主要給大家介紹了關(guān)于C++中繼承與多態(tài)的基礎(chǔ)虛函數(shù)類的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
c++ 動(dòng)態(tài)內(nèi)存分配相關(guān)總結(jié)
這篇文章主要介紹了c++ 動(dòng)態(tài)內(nèi)存分配相關(guān)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)和使用c++,感興趣的朋友可以了解下2021-02-02
C++實(shí)現(xiàn)稀疏矩陣的壓縮存儲(chǔ)實(shí)例
本篇文章主要介紹了C++實(shí)現(xiàn)稀疏矩陣的壓縮存儲(chǔ)實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06

