另一种判断文件存在的方法--_access和_waccess

_access, _waccess

Determine file-access permission.

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

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

RoutineRequired HeaderOptional HeadersCompatibility
_access<io.h><errno.h>Win 95, Win NT
_waccess<wchar.h> or <io.h><errno.h>Win NT

 

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIBSingle thread static library, retail version
LIBCMT.LIBMultithread static library, retail version
MSVCRT.LIBImport library for MSVCRT.DLL, retail version

 

Return Value

Each of these functions returns 0 if the file has the given mode. The function returns –1 if the named file does not exist or is not accessible in the given mode; in this case, errno is set as follows:

EACCES

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

ENOENT

Filename or path not found.

Parameters

path

File or directory path

mode

Permission setting

Remarks

When used with files, the _access function determines whether the specified file exists and can be accessed as specified by the value of mode. When used with directories, _access determines only whether the specified directory exists; in Windows NT, all directories have read and write access.

mode ValueChecks File For
00Existence only
02 Write permission
04Read permission
06Read and write permission

 

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

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined_MBCS Defined_UNICODE Defined
_taccess_access_access_waccess

 

Example

/* ACCESS.C: This example uses _access to check the
 * file named "ACCESS.C" to see if it exists and if
 * writing is allowed.
 */

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

void main( void )
{
	/* Check for existence */
	if( (_access( "ACCESS.C", 0 )) != -1 )
	{
		printf( "File ACCESS.C exists\n" );
		/* Check for write permission */
		if( (_access( "ACCESS.C", 2 )) != -1 )
			printf( "File ACCESS.C has write permission\n" );
		if( (_access( "ACCESS.C", 4 )) != -1 )
			printf( "File ACCESS.C has read permission\n" );
		if( (_access( "ACCESS.C", 6 )) != -1 )
			printf( "File ACCESS.C has read and write permission\n" );
	}
	else
	{
		printf("File ACCESS.C does not exists\n");
	}
}

转载于:https://www.cnblogs.com/joeblackzqq/archive/2010/11/09/1872518.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值