1. 程式人生 > 其它 >C語言常用函式-access()檔案訪問許可權設定函式

C語言常用函式-access()檔案訪問許可權設定函式

演示版本

VS2012

  • access()函式

access()函式用於確定檔案的訪問許可權。

語法

int access(const char *file, int auth);

access()函式的語法引數說明如下:

引數file為檔名。

引數auth值為0時測試檔案是否存在,值為2時測試檔案是否可寫。

access()測試結果:允許時返回0,否則返回-1。

示例

本示例演示用access()函式確定檔案是否存在,程式執行前保證1.txt存在,2.txt不存在。

其具體程式碼如下:

#include <stdio.h>
#include <io.h>

int isExist(char
* filename)//定義子函式判斷檔案是否存在 { return _access(filename, 0)==0;//返回0表示檔案存在 } int main() { char *f1 = "D:\\1\\1\\1.txt";//第一個檔案 char *f2 = "D:\\1\\1\\2.txt";//第二個檔案 if (isExist(f1))//呼叫子函式,如果存在 printf("%s exist\n", f1);//顯示檔案存在 else printf("%s does not exist\n", f1);//顯示檔案不存在 if
(isExist(f2))//呼叫子函式,如果存在 printf("%s exist\n", f2);//顯示檔案存在 else printf("%s does not exist\n", f2);//顯示檔案不存在 }

附加資料

https://www.cnblogs.com/nxopen2018/p/12182137.html

阿飛

2021年8月2日