Exportar (0) Imprimir
Expandir todo
div
eof
Expandir Minimizar

_access_s, _waccess_s

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


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.

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. In this case, the function returns an error code from the set as follows and also sets errno to the same value.

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, see errno, _doserrno, _sys_errlist, and _sys_nerr.

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 NT 4.0 and Windows 2000, 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 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.

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

Generic-Text Routine Mappings
Tchar.h routine _UNICODE and _MBCS not defined _MBCS defined _UNICODE defined

_taccess_s

_access_s

_access_s

_waccess_s

Routine Required header Optional header Compatibility

_access_s

<io.h>

<errno.h>

ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

_waccess_s

<wchar.h> or <io.h>

<errno.h>

Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

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

Output

File crt_access_s.c exists.
File crt_access_s.c does not have write permission.

Adiciones de comunidad

AGREGAR
Mostrar:
© 2014 Microsoft