Skip to content

Latest commit

 

History

History
122 lines (92 loc) · 4.88 KB

access-waccess.md

File metadata and controls

122 lines (92 loc) · 4.88 KB
description title ms.date api_name api_location api_type topic_type f1_keywords helpviewer_keywords ms.assetid
Learn more about: _access, _waccess
_access, _waccess
4/2/2020
_access
_waccess
_o__access
_o__waccess
msvcrt.dll
msvcr80.dll
msvcr90.dll
msvcr100.dll
msvcr100_clr0400.dll
msvcr110.dll
msvcr110_clr0400.dll
msvcr120.dll
msvcr120_clr0400.dll
ucrtbase.dll
api-ms-win-crt-filesystem-l1-1-0.dll
api-ms-win-crt-private-l1-1-0.dll
DLLExport
apiref
_waccess
_access
taccess
waccess
_taccess
access function
_taccess function
waccess function
_access function
_waccess function
taccess function
ba34f745-85c3-49e5-a7d4-3590bd249dd3

_access, _waccess

Determines if a file is read-only or not. More secure versions are available; see _access_s, _waccess_s.

Syntax

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

Parameters

path
File or directory path.

mode
Read/write attribute.

Return Value

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.

Value Description
EACCES Access denied: the file's permission setting does not allow specified access.
ENOENT File name or path not found.
EINVAL Invalid parameter.

For more information about these and other return codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

Remarks

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. 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.

mode value Checks file for
00 Existence only
02 Write-only
04 Read-only
06 Read 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.

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

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.

By default, this function's global state is scoped to the application. To change this, see Global state in the CRT.

Generic-Text Routine Mappings

Tchar.h routine _UNICODE and _MBCS not defined _MBCS defined _UNICODE defined
_taccess _access _access _waccess

Requirements

Routine Required header Optional headers
_access <io.h> <errno.h>
_waccess <wchar.h> or <io.h> <errno.h>

Example

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

// 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.

See also

File Handling
_chmod, _wchmod
_fstat, _fstat32, _fstat64, _fstati64, _fstat32i64, _fstat64i32
_open, _wopen
_stat, _wstat Functions