C語言中access/_access函數(shù)的用法

本篇內(nèi)容介紹了“C語言中access/_access函數(shù)的用法”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站制作、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、天鎮(zhèn)ssl等。為千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的天鎮(zhèn)網(wǎng)站制作公司

在Linux下,access函數(shù)的聲明在<unistd.h>文件中,聲明如下:

int access(const char *pathname, int mode);

access函數(shù)用來判斷指定的文件或目錄是否存在(F_OK),已存在的文件或目錄是否有可讀(R_OK)、可寫(W_OK)、可執(zhí)行(X_OK)權(quán)限。F_OK、R_OK、W_OK、X_OK這四種方式通過access函數(shù)中的第二個(gè)參數(shù)mode指定。如果指定的方式有效,則此函數(shù)返回0,否則返回-1。

在Windows下沒有access函數(shù),但在<io.h>文件中有_access函數(shù),聲明如下:

int _access(const char* _Filename, int _AccessMode);

windows下的函數(shù)_access與linux下的access函數(shù)功能類似,用來判斷指定的文件或目錄是否僅存在(00),已存在的文件或目錄是否有僅讀(04)、僅寫(02)、既可讀又可寫(06)權(quán)限。這四種方式通過_access函數(shù)中的第二個(gè)參數(shù)mode指定,如果mode傳入的值不是0或2或4或6,調(diào)用此函數(shù)則會(huì)crash。如果指定的方式有效,則此函數(shù)返回0,否則返回-1。

以下是測試代碼(access.cpp):

#include "access.hpp"#include <iostream>#include <vector>#include <string>#ifdef _MSC_VER#include <io.h>#else#include <unistd.h>#endifnamespace 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

“C語言中access/_access函數(shù)的用法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

本文名稱:C語言中access/_access函數(shù)的用法
標(biāo)題路徑:http://www.muchs.cn/article16/jidogg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)軟件開發(fā)、品牌網(wǎng)站建設(shè)電子商務(wù)、網(wǎng)站營銷網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)