_access()函数检测文件或文件夹是否存在,_mkdir()创建目录。
2017-06-18 12:02
661 查看
MSDN 对于_access()函数的介绍:
int _access( const char *path, int mode ); Required header:<io.h>,Optional headers:<errno.h>
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
Creates a new directory.
Required header : <direct.h>
Return Value
Each of these functions returns the value 0 if the new directory was created. On an error, the function returns –1 and sets errno as follows
Example
int _access( const char *path, int mode ); Required header:<io.h>,Optional headers:<errno.h>
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
/* 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 " ); /* Check for write permission */ if( (_access( "ACCESS.C", 2 )) != -1 ) printf( "File ACCESS.C has write permission " ); } }
Creates a new directory.
int _mkdir( const char *dirname );
Required header : <direct.h>
Return Value
Each of these functions returns the value 0 if the new directory was created. On an error, the function returns –1 and sets errno as follows
Example
// crt_makedir.c #include <direct.h> #include <stdlib.h> #include <stdio.h> int main( void ) { if( _mkdir( "\\testtmp" ) == 0 ) { printf( "Directory '\\testtmp' was successfully created\n" ); system( "dir \\testtmp" ); if( _rmdir( "\\testtmp" ) == 0 ) printf( "Directory '\\testtmp' was successfully removed\n" ); else printf( "Problem removing directory '\\testtmp'\n" ); } else printf( "Problem creating directory '\\testtmp'\n" ); }
相关文章推荐
- C语言判断文件夹或者文件是否存在以及权限等信息access函数和删除文件或目录 remove()函数使用实例
- android java 检测文件夹(目录)是否存在,不存在则创建
- 获取文件扩展名、 文件名、文件名不包括扩展名,检测文件夹、文件是否存在、创建文件夹,检测文件夹状态、检查文件名是否符合要求
- C# 判断文件和文件夹是否存在并创建
- C#判断文件及文件夹是否存在并创建
- C中判断目录,文件是否存在,创建目录,求目录或文件大小的方法(转)
- 判断所指定的文件或目录(文件夹)是否存在存储过程
- VC中判断目录,文件是否存在,创建目录的方法
- 判断文件和文件夹是否存在并创建
- 检测一个目录或文件是否存在
- 检测文件夹是否存在,不存在则创建
- 自动检测创建多级目录, fso与stream生成文件函数
- Android 检测目录是否存在,不存在则创建
- VC中判断目录,文件是否存在,创建目录,求目录或文件大小的方法
- VC中判断目录,文件是否存在,创建目录的方法
- 文件磁盘相关函数[6]-判断文件夹是否存在 DirectoryExists
- VC中判断目录,文件是否存在,创建目录的方法
- 判断所指定的文件或目录(文件夹)是否存在存储过程
- 用_access判断文件或文件夹是否存在及其读写权限
- VC中判断目录,文件是否存在,创建目录,求目录或文件大小的方法