C語言判斷文件是否存在(轉)


 

int   access(const   char   *filename,   int   amode);

amode參數為0時表示檢查文件的存在性,如果文件存在,返回0,不存在,返回-1。

這個函數還可以檢查其它文件屬性:

06     檢查讀寫權限 

04     檢查讀權限 

02     檢查寫權限 

01     檢查執行權限 

00     檢查文件的存在性

在UNIX和VC下實驗成功。 好處是 fopen(..,"r")不好,當無讀權限時一不行了。

而這個就算這個文件沒有讀權限,也可以判斷這個文件存在於否 存在返回0,不存在返回-1

 1 #include <stdio.h>
 2 int main()  3 {  4  5 printf ("%d",access("111",0));  6  7 --------------------------------------------------------------------------------------------  8  9 #include <io.h> 10 #include <stdio.h> 11 #include <stdlib.h> 12 13 void main( void ) 14 { 15 /* Check for existence */ 16 if( (_access( "ACCESS.C", 0 )) != -1 ) 17  { 18 printf( "File ACCESS.C exists\n" ); 19 /* Check for write permission */ 20 if( (_access( "ACCESS.C", 2 )) != -1 ) 21 printf( "File ACCESS.C has write permission\n" ); 22  } 23 } 24 25 26 Output 27 28 File ACCESS.C exists 29 File ACCESS.C has write permission

 

 

轉自:http://www.cnblogs.com/lancidie/archive/2011/06/30/2094924.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM