acess() 判断目录是否存在
acess()功能描述:
檢查調(diào)用進(jìn)程是否可以對指定的文件執(zhí)行某種操作。
<pre lang="c" escaped="true">
#include <unistd.h>
int access(const char *pathname, int mode);
</pre>
參數(shù)說明:
pathname: 需要測試的文件路徑名。
mode: 需要測試的操作模式,可能值是一個(gè)或<strong>多個(gè)</strong>.
<ol>
<li>R_OK(可讀?),</li>
<li> W_OK(可寫?), </li>
<li>X_OK(可執(zhí)行?) </li>
<li>或 F_OK(文件存在?)組合體。 </li>
</ol>
?
<blockquote>其實(shí)在用的最多的主要是利用F_OK來檢查目錄是否存在。</blockquote>
?
返回說明:
成功執(zhí)行時(shí),返回0。失敗返回-1,errno被設(shè)為以下的某個(gè)值
<ol>
<li>EINVAL: 模式值無效 </li>
<li>EACCES: 文件或路徑名中包含的目錄不可訪問 </li>
<li>ELOOP : 解釋路徑名過程中存在太多的符號(hào)連接 </li>
<li>ENAMETOOLONG:路徑名太長 </li>
<li>ENOENT: 路徑名中的目錄不存在或是無效的符號(hào)連接 </li>
<li>ENOTDIR: 路徑名中當(dāng)作目錄的組件并非目錄 </li>
<li>EROFS: 文件系統(tǒng)只讀 </li>
<li>EFAULT: 路徑名指向可訪問的空間外 </li>
<li>EIO: 輸入輸出錯(cuò)誤 </li>
<li>ENOMEM: 不能獲取足夠的內(nèi)核內(nèi)存 </li>
<li>ETXTBSY:對程序?qū)懭氤鲥e(cuò) </li>
</ol>
<pre lang="c" escaped="true" line="1">
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("Usage: ./test filename\n");
exit(1);
}
if (access(argv[1], F_OK) == -1) {
puts("File not exists!");
exit(2);
}
if (access(argv[1], R_OK) == -1)
puts("You can't read the file!");
else
if (access(argv[1], R_OK | W_OK) != -1)
puts("You can read and write the file");
else
puts("You can read the file");
exit(0);
}
</pre>
?
轉(zhuǎn)載于:https://www.cnblogs.com/liweilijie/p/4984101.html
總結(jié)
以上是生活随笔為你收集整理的acess() 判断目录是否存在的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 的23种设计模式 之单身狗和隔
- 下一篇: 11gR2 RAC时间同异常导致节点do