_access, _waccess 和 _access_s, _waccess_s

转自MicroSolft文档:https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/access-waccess?view=vs-2019

_access,_waccess

确定文件是否为只读。提供更安全的版本;参见_access_s, _waccess_s.

句法

C

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

参数

path:文件或目录路径;

mode:读/写属性。

返回值

Each function returns 0 if the file has the given mode. The function returns -1 if the named file does not exist or does not have the given mode; in this case, errno is set as shown in the following table.

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

 
返回值系统错误信息 
EACCES没有权限13

Access denied: the file's permission setting does not allow specified access.

拒绝访问:文件的权限设置不允许指定的访问。

ENOENT无此文件或目录2

File name or path not found.

找不到文件名或路径。

EINVAL无效的参数22

Invalid parameter.

无效的参数。

 

备注

When used with files, the _access function determines whether the specified file or directory exists and has the attributes specified by the value of mode. 与文件一起使用时,_access函数确定指定的文件或目录是否存在,并具有由mode值指定的属性。

When used with directories, _access determines only whether the specified directory exists; in Windows 2000 and later operating systems, all directories have read and write access.与目录一起使用时,_access仅确定指定的目录是否存在;在Windows 2000和更高版本的操作系统中,所有目录都具有读写访问权限。

 
mode 值检查文件
00Existence only
02Write-only
04Read-only
06Read and write

This function only checks whether the file and directory are read-only or not, it does not check the filesystem security settings.

此功能仅检查文件和目录是否为只读,而不检查文件系统安全设置。

For that you need an access token. For more information on filesystem security, see Access Tokens. An ATL class exists to provide this functionality; see CAccessToken Class.

为此,您需要访问令牌。有关文件系统安全性的更多信息,请参见访问令牌。存在一个ATL类来提供此功能。参见CAccessToken类

_waccess is a wide-character version of _access; the path argument to _waccess is a wide-character string. _waccess and _access behave identically otherwise.

_waccess是宽字符版本_access ; _waccesspath参数是一个宽字符字符串。_waccess_access的行为相同。

This function validates its parameters. If path is NULL or mode does not specify a valid mode, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, the function sets errno to EINVAL and returns -1.

此功能验证其参数。如果path为NULL或mode未指定有效模式,则将调用无效的参数处理程序,如Parameter Validation 中所述。如果允许继续执行,函数将设置errnoEINVAL并返回-1。

通用文本例程映射

 
Tchar.h routine_UNICODE and _MBCS not defined_MBCS defined_UNICODE defined
_taccess_access_access_waccess

要求

 
Routine必要头文件可选头文件
_access<io.h><errno.h>
_waccess<wchar.h> or <io.h><errno.h>

示例代码

The following example uses _access to check the file named crt_ACCESS.C to see whether it exists and whether writing is allowed.

下面的示例使用_access来检查名为crt_ACCESS.C的文件,以查看该文件是否存在以及是否允许写入。

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

输出量

File crt_ACCESS.C exists.
File crt_ACCESS.C does not have write permission.

_access_s,_waccess_s

Determines file read/write permissions. This is a version of _access, _waccess with security enhancements as described in Security Features in the CRT.

确定文件读/写权限。这是_access,_waccess的版本安全性增强功能

句法

C

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

参数

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

mode:Permission setting.权限设置。

返回值

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.如果文件具有给定模式,则每个函数返回0。如果命名文件不存在或在给定模式下不可访问,该函数将返回错误代码。

In this case, the function returns an error code from the set as follows and also sets errno to the same value.在这种情况下,该函数按如下所示从集合中返回错误代码,并将其设置为errno相同的值。

 
错误值系统信息代码描述
EACCES没有权限13

Access denied. The file's permission setting does not allow specified access.

拒绝访问。文件的权限设置不允许指定的访问。

ENOENT无此文件或目录2File name or path not found.
EINVAL无效的参数22Invalid parameter.

备注

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. When used with directories, _access_s determines only whether the specified directory exists. 与文件一起使用时,_access_s函数确定指定的文件是否存在以及是否可以根据mode的值进行访问。与目录一起使用时,_access_s仅确定指定的目录是否存在。、

In Windows 2000 and later operating systems, all directories have read and write access.在Windows 2000和更高版本的操作系统中,所有目录都具有读写访问权限。

 
mode valueChecks file for
00Existence only.仅存在
02Write permission.写入权限
04Read permission.阅读权限
06Read and write permission.读写权限

Permission to read or write the file is not enough to ensure the ability to open a file. For example, if a file is locked by another process, it might not be accessible even though _access_s returns 0.

读取或写入文件的权限不足以确保能够打开文件。例如,如果文件被另一个进程锁定,则即使_access_s返回0 ,也可能无法访问该文件。

_waccess_s is a wide-character version of _access_s, where the path argument to _waccess_s is a wide-character string. Otherwise, _waccess_s and _access_s behave identically.

_waccess_s是宽字符版本_access_s,其中该路径参数_waccess_s是宽字符串。否则,_waccess_s_access_s的行为相同。

These functions validate their parameters. If path is NULL or mode does not specify a valid mode, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions set errno to EINVAL and return EINVAL.

这些功能验证其参数。如果path为NULL或mode未指定有效模式,则将调用无效的参数处理程序,如Parameter Validation中所述。如果允许执行继续,这些函数将设置errnoEINVAL并返回EINVAL

通用文本例程映射

 
Tchar.h routine_UNICODE and _MBCS not defined_MBCS defined_UNICODE defined
_taccess_s_access_s_access_s_waccess_s

要求

 
Routine必选头文件可选头文件
_access_s<io.h><errno.h>
_waccess_s<wchar.h> or <io.h><errno.h>

Example

This example uses _access_s to check the file named crt_access_s.c to see whether it exists and whether writing is allowed.

本示例使用_access_s检查名为crt_access_s.c的文件以查看其是否存在以及是否允许写入。

C

// 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.
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值