_access, _waccess

确定文件是否为只读。 更安全版本可用; _access_s, _waccess_s参见。

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

参数

  • path
    文件或目录路径。

  • mode
    读/写属性。

返回值

文件,则与给定模式,每个函数返回 0。 函数返回 – 1; 如果名称文件不存在或不具有给定模式;在这种情况下,如下表所示, errno 设置。

  • EACCES
    访问被拒绝:文件的权限集不允许指定的访问。

  • ENOENT
    未找到的文件名或路径。

  • EINVAL
    参数无效。

有关这些属性和其他的更多信息返回代码示例,请参见 _doserrno、 errno、 _sys_errlist 和 _sys_nerr

备注

当用于文件, _access 函数确定指定的文件或目录是否存在且具有 mode的值指定的属性。 当使用与目录, _access 来确定指定的目录是否存在; 只有在 Windows 2000 和更高版本的操作系统,所有目录读取和写入。

mode 值

检查文件。

00

仅存在

02

只读

04

只读

06

读取和写入

此功能才检查是否文件和目录是只读的,它不检查文件系统安全设置。 对于已需要访问标记。 有关文件系统安全性的更多信息,请参见 访问令牌。 ATL 类存在提供此功能; CAccessToken选件类参见。

_waccess_access的宽字符版本;为 _waccess 的 path 参数是宽字符字符串。 _waccess_access 否则具有相同的行为。

此功能验证其参数。 如果 path 是 NULL 或 mode 不指定有效的模式,无效参数调用处理程序,如 参数验证所述。 如果执行允许继续,该功能集 errno 到 EINVAL 并返回 -1。

一般文本例程映射

Tchar.h 实例

未定义的 _UNICODE 和 _MBCS

定义的 _MBCS

定义的 _UNICODE

_taccess

_access

_access

_waccess

要求

实例

必需的头

可选标头

_access

io.h

errno.h

_waccess

wchar.h 或 io.h

errno.h

示例

下面的示例使用 _access 检查名为 crt_ACCESS.C 的文件看它是否存在,并编写是否允许。

// 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" );
    }
}
      

.NET Framework 等效项

System.IO.FileAccess

请参见

参考

文件处理

_chmod, _wchmod

_fstat, _fstat32, _fstat64, _fstati64, _fstat32i64, _fstat64i32

_open, _wopen

_stat, _wstat功能