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