_access_s, _waccess_s, _taccess_s

Determines file read/write permissions. These functions are versions of _access, _waccess with security enhancements as described in Security features in the CRT.

For _taccess_s, see Generic-text function mappings.

Syntax

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

Parameters

path
File or directory path.

mode
Permission setting.

Return value

Each function returns 0 if the file has the given mode. The function returns an error code if the named file doesn't exist or isn't accessible in the given mode. In this case, the function returns an error code from the set as follows and also sets errno to the same value.

errno value Condition
EACCES Access denied. The file's permission setting doesn't allow specified access.
ENOENT File name or path not found.
EINVAL Invalid parameter.

For more information, see errno, _doserrno, _sys_errlist, and _sys_nerr.

Remarks

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. In Windows 2000 and later operating systems, all directories have read and write access.

mode value Checks file for
00 Existence only.
02 Write permission.
04 Read permission.
06 Read and write permission.

Permission to read or write the file isn't 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.

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

These functions validate their parameters. If path is NULL or mode doesn't 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.

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

Generic-text function mappings

The function in the tchar.h column maps to the function in the other columns depending on the character set that is defined at compile time.

tchar.h function _UNICODE and _MBCS not defined _MBCS defined _UNICODE defined
_taccess_s _access_s _access_s _waccess_s

Requirements

Routine Required header Optional header
_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.

// 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
_chmod, _wchmod
_fstat, _fstat32, _fstat64, _fstati64, _fstat32i64, _fstat64i32
_open, _wopen
_stat, _wstat functions