[转载]判断文件是否存在


来自:http://blog.chinaunix.net/uid-29121609-id-3886397.html
_access() 检验文件的模式
返回值:0表示允许访问,-1表示禁止访问。
函数原型:
int _access( 
   const char *path, 
   int mode 
);
另外有宽字符的:
int _waccess( 
   const wchar_t *path, 
   int mode 
);
mode value Checks file for

00

Existence only

02

Write-only

04

Read-only

06

Read and write

这个函数只检查文件和目录是否是只读的,它不检查文件系统安全设置。

返回值 文件具有给定的模式返回0,如果文件不存在或没有给定的模式,返回-1

 

EACCESS(拒绝访问:文件的权限设置不允许指定的访问),

ENOENT(文件名或路径未找到),

EINVAL(无效的参数).

 

例子:

// crt_access.c
// compile with: /W1
// This example uses _access to check the file named
// crt_ACCESS.C to see if it exists and if writing is allowed.

#include  <io.h>
#include  <stdio.h>
#include  <stdlib.h>

int main( void )
{
    // Check for existence.
    if( (_access( "crt_ACCESS.C", 0 )) != -1 )
    {
        printf_s( "File crt_ACCESS.C exists.\n" );

        // Check for write permission.
        // Assume file is read-only.
        if( (_access( "crt_ACCESS.C", 2 )) == -1 )
            printf_s( "File crt_ACCESS.C does not have write permission.\n" );
    }
}
posted on 2015-04-07 14:38  wusichen  阅读(162)  评论(0编辑  收藏  举报