法: int access(const char *filename, int

amode);

以前一直没用过这个函数,今天调试程序发现了这个函数,感觉挺好用,尤其是判断一个文件或文件夹是否存在的时候,用不着再find了,文件的话还可以检测读写权限,文件夹的话则只能判断是否存在,下面摘自MSDN:

int _access( const char *path, int mode );

Return Value

Each of these functions returns 0 if the file has the given

mode. The function returns –1 if the named file does not exist or

is not accessible in the given mode; in this case, errno is set as

follows:

EACCES

Access denied: file’s permission setting does not allow

specified access.

ENOENT

Filename or path not found.

Parameters

path

File or directory path

mode

Permission setting

Remarks

When used with files, the _access function determines whether

the specified file exists and can be accessed as specified by the

value of mode. When used with directories, _access determines only

whether the specified directory exists; in Windows NT, all

directories have read and write access.

mode

Value Checks File For

00 Existence only

02 Write permission

04 Read permission

06 Read and write permission

Example

#include 

#include 

#include 

void main( void )

{

if(

(_access( "ACCESS.C", 0 )) != -1

)

{

printf( "File ACCESS.C exists "

);

if( (_access( "ACCESS.C", 2 )) != -1

)

printf( "File ACCESS.C has write permission "

);

}

}

Output

File ACCESS.C exists

File ACCESS.C has write permission

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐