_access_s、_waccess_s_access_s, _waccess_s

确定文件的读取/写入权限。Determines file read/write permissions. 这是 _access、_waccess 版本,具有 CRT 中的安全功能 中所述的安全增强功能。This is a version of _access, _waccess with security enhancements as described in Security Features in the CRT.

语法Syntax

errno_t _access_s(
   const char *path,
   int mode
);
errno_t _waccess_s(
   const wchar_t *path,
   int mode
);

参数Parameters

pathpath
文件或目录路径。File or directory path.

模式mode
权限设置。Permission setting.

返回值Return Value

如果该文件具有给定的模式,则每个函数将返回 0。Each function returns 0 if the file has the given mode. 如果命名文件不存在或无法以指定模式进行访问,则该函数将返回错误代码。The function returns an error code if the named file does not exist or is not accessible in the given mode. 在这种情况下,该函数从如下集合中返回错误代码,并同时将 errno 设置为相同的值。In this case, the function returns an error code from the set as follows and also sets errno to the same value.

errno 值errno value 条件Condition
EACCES 访问被拒绝。Access denied. 文件的权限设置不允许指定的访问权限。The file's permission setting does not allow specified access.
ENOENT 未找到文件名或路径。File name or path not found.
EINVAL 参数无效。Invalid parameter.

有关详细信息,请参阅 errno、_doserrno、_sys_errlist 和 _sys_nerrFor more information, see errno, _doserrno, _sys_errlist, and _sys_nerr.

备注Remarks

与文件一起使用时 _access_s函数确定指定的文件是否存在并且可作为访问指定的值模式When used with files, the _access_s function determines whether the specified file exists and can be accessed as specified by the value of mode. 与目录一起使用时 _access_s仅确定指定的目录是否存在。When used with directories, _access_s determines only whether the specified directory exists. 在 Windows 2000 和更高版本操作系统中,所有目录具有读取和写入访问权限。In Windows 2000 and later operating systems, all directories have read and write access.

模式值mode value 文件检查内容Checks file for
0000 仅检查是否存在。Existence only.
0202 写入权限。Write permission.
0404 读取权限。Read permission.
0606 读取和写入权限。Read and write permission.

读取或写入文件的权限不足以确保文件的打开功能。Permission to read or write the file is not enough to ensure the ability to open a file. 例如,如果文件被另一个进程锁定,它可能不能访问即使 _access_s返回 0。For example, if a file is locked by another process, it might not be accessible even though _access_s returns 0.

_waccess_s是宽字符版本 _access_s,其中路径参数 _waccess_s是宽字符字符串。_waccess_s is a wide-character version of _access_s, where the path argument to _waccess_s is a wide-character string. 否则为 _waccess_s_access_s行为方式相同。Otherwise, _waccess_s and _access_s behave identically.

这些函数验证其参数。These functions validate their parameters. 如果路径为 NULL 或模式未指定有效的模式下,将调用无效参数处理程序,如中所述参数验证If path is NULL or mode does not specify a valid mode, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则这些功能将 errno 设置为 EINVAL 并返回 EINVALIf execution is allowed to continue, these functions set errno to EINVAL and return EINVAL.

一般文本例程映射Generic-Text Routine Mappings

Tchar.h 例程Tchar.h routine 未定义 _UNICODE 和 _MBCS_UNICODE and _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_taccess_s _access_s_access_s _access_s_access_s _waccess_s_waccess_s

要求Requirements

例程所返回的值Routine 必需的标头Required header 可选标头Optional header
_access_s_access_s <io.h><io.h> <errno.h><errno.h>
_waccess_s_waccess_s <wchar.h> 或 <io.h><wchar.h> or <io.h> <errno.h><errno.h>

示例Example

此示例使用 _access_s来检查名为 crt_access_s.c 若要查看其是否存在以及是否允许写入的文件。This example uses _access_s to check the file named crt_access_s.c to see whether it exists and whether writing is allowed.

// crt_access_s.c

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

int main( void )
{
    errno_t err = 0;

    // Check for existence.
    if ((err = _access_s( "crt_access_s.c", 0 )) == 0 )
    {
        printf_s( "File crt_access_s.c exists.\n" );

        // Check for write permission.
        if ((err = _access_s( "crt_access_s.c", 2 )) == 0 )
        {
            printf_s( "File crt_access_s.c does have "
                      "write permission.\n" );
        }
        else
        {
            printf_s( "File crt_access_s.c does not have "
                      "write permission.\n" );
        }
    }
    else
    {
        printf_s( "File crt_access_s.c does not exist.\n" );
    }
}
File crt_access_s.c exists.
File crt_access_s.c does not have write permission.

请参阅See also

文件处理File Handling
_access、_waccess_access, _waccess
_chmod、_wchmod_chmod, _wchmod
_fstat、_fstat32、_fstat64、_fstati64、_fstat32i64、_fstat64i32_fstat, _fstat32, _fstat64, _fstati64, _fstat32i64, _fstat64i32
_open、_wopen_open, _wopen
_stat、_wstat 函数_stat, _wstat Functions