linux下的access()函數判斷檔案是否存在、列印時間

來源:互聯網
上載者:User

一、access函數

功能描述:
檢查調用進程是否可以對指定的檔案執行某種操作。
 
用法:
#include <unistd.h>
#include <fcntl.h>

int access(const char *pathname, int mode);  
 
參數:
pathname: 需要測試的檔案路徑名。  
mode: 需要測試的操作模式,可能值是一個或多個R_OK(可讀?), W_OK(可寫?), X_OK(可執行?) 或 F_OK(檔案存在?)組合體。
 
返回說明:
成功執行時,返回0。失敗返回-1,errno被設為以下的某個值
EINVAL: 模式值無效  
EACCES: 檔案或路徑名中包含的目錄不可訪問
ELOOP : 解釋路徑名過程中存在太多的符號串連
ENAMETOOLONG:路徑名太長
ENOENT:  路徑名中的目錄不存在或是無效的符號串連
ENOTDIR: 路徑名中當作目錄的組件並非目錄
EROFS: 檔案系統唯讀
EFAULT: 路徑名指向可訪問的空間外
EIO:  輸入輸出錯誤
ENOMEM: 不能擷取足夠的核心記憶體
ETXTBSY:對程式寫入出錯

 

例如:

C代碼  
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <unistd.h>  
  4. #include <fcntl.h>  
  5.   
  6. int main()  
  7. {  
  8.     if((access("test.c",F_OK))!=-1)  
  9.     {  
  10.         printf("檔案 test.c 存在.\n");  
  11.     }  
  12.     else  
  13.     {  
  14.         printf("test.c 不存在!\n");  
  15.     }  
  16.   
  17.     if(access("test.c",R_OK)!=-1)  
  18.     {  
  19.         printf("test.c 有可讀許可權\n");  
  20.     }  
  21.     else  
  22.     {  
  23.         printf("test.c 不可讀.\n");  
  24.     }  
  25.   
  26.     if(access("test.c",W_OK)!=-1)  
  27.     {  
  28.         printf("test.c 有可寫入權限\n");  
  29.     }  
  30.     else  
  31.     {  
  32.         printf("test.c 不可寫.\n");  
  33.     }  
  34.     if(access("test.c",X_OK)!=-1)  
  35.     {  
  36.         printf("test.c 有可執行許可權\n");  
  37.     }  
  38.     else  
  39.     {  
  40.         printf("test.c 不可執行.\n");  
  41.     }  
  42.   
  43.     return 0;  
  44. }  
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>int main(){if((access("test.c",F_OK))!=-1){printf("檔案 test.c 存在.\n");}else{printf("test.c 不存在!\n");}if(access("test.c",R_OK)!=-1){printf("test.c 有可讀許可權\n");}else{printf("test.c 不可讀.\n");}if(access("test.c",W_OK)!=-1){printf("test.c 有可寫入權限\n");}else{printf("test.c 不可寫.\n");}if(access("test.c",X_OK)!=-1){printf("test.c 有可執行許可權\n");}else{printf("test.c 不可執行.\n");}return 0;}

 運行結果如下:

二、如果在C中列印目前時間

下面是一個列印系統目前時間的小例子,函數的文法暫時就不列出了,只是會用這些就差不多了

C代碼  
  1. #include <stdio.h>  
  2. #include <time.h>  
  3. int main()  
  4. {  
  5.     time_t now = time(NULL);  
  6.     char buf[25];  
  7.     strftime(buf,24,"%Y%m%d",localtime(&now));  
  8.     printf("%s\n",buf);  
  9.   
  10.     strftime(buf,24,"%Y-%m-%d %H:%M:%S",localtime(&now));  
  11.     printf("%s\n",buf);  
  12.   
  13.     strftime(buf,24,"%y%m%d %H:%M:%S",localtime(&now));  
  14.     printf("%s\n",buf);  
  15.   
  16.     strftime(buf,24,"%y%m%d",localtime(&now));  
  17.     printf("%s\n",buf);  
  18.   
  19.     strftime(buf,24,"%H:%M:%S",localtime(&now));  
  20.     printf("%s\n",buf);  
  21.     return 0;  
  22. }  
#include <stdio.h>#include <time.h>int main(){time_t now = time(NULL);char buf[25];strftime(buf,24,"%Y%m%d",localtime(&now));printf("%s\n",buf);strftime(buf,24,"%Y-%m-%d %H:%M:%S",localtime(&now));printf("%s\n",buf);strftime(buf,24,"%y%m%d %H:%M:%S",localtime(&now));printf("%s\n",buf);strftime(buf,24,"%y%m%d",localtime(&now));printf("%s\n",buf);strftime(buf,24,"%H:%M:%S",localtime(&now));printf("%s\n",buf);return 0;}

 運行結果:

  • 大小: 14.6 KB

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.