博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
另一种判断文件存在的方法--_access和_waccess
阅读量:3974 次
发布时间:2019-05-24

本文共 2324 字,大约阅读时间需要 7 分钟。

_access, _waccess

Determine file-access permission.

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

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

Routine Required Header Optional Headers Compatibility
_access <io.h> <errno.h> Win 95, Win NT
_waccess <wchar.h> or <io.h> <errno.h> Win NT

 

For additional compatibility information, see in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import 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 Value Checks File For
00 Existence only
02 Write permission
04 Read permission
06 Read 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"
);
    
}
}

转载地址:http://lxhki.baihongyu.com/

你可能感兴趣的文章
Android WebView Long Press长按保存图片到手机
查看>>
BaseAnimation是基于开源的APP,致力于收集各种动画效果(最新版本1.3)
查看>>
TextView显示html图片点击图片放大等操作
查看>>
【Android】自定义控件让TextView的drawableLeft与文本一起居中显示
查看>>
Android Fragment getActivity返回null解决
查看>>
Android(视频、图片)加载和缓存类库Glide
查看>>
Android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据
查看>>
Android音频系统之AudioPolicyService
查看>>
Android系统Root与静默安装
查看>>
Android Property实现介绍
查看>>
Android SystemProperties设置/取得系统属性的用法总结
查看>>
Android 休眠 FLAG_KEEP_SCREEN_ON
查看>>
Android添加onKeyLongPress事件
查看>>
使用微信api将内容分享给好友,或者发送到朋友圈
查看>>
android开发中输入法的弹出和隐藏
查看>>
Android 如何在自定义界面上启用输入法 (How to enable inputmethod for the custom UI)
查看>>
Android MediaCodec小结
查看>>
YUV格式说明
查看>>
MediaCodec and Camera: colorspaces don't match
查看>>
android adb 读写模式 挂载文件系统
查看>>