##########################################################
判断文件夹是否存在:
在windows环境下头文件为:
- #include <io.h>
在linux环境下头文件为:
- #include <unistd.h>
- <pre name="code" class="cpp">int access(const char* _Filename, int _AccessMode)
该函数功能为确定文件或文件夹的访问权限,如果指定的访问权限有效,则函数返回0,否则返回-1
Filename可以是文件路径,也可以是文件夹路径,可以使用绝对路径或相对路径
_AccessMode表示要验证的文件访问权限,有可读、可写、可执行以及是否存在四种权限,当Filename表示文件夹时仅能查询文件夹是否存在
_AccessMode:
头文件unistd.h中有如下定义:
- #define R_OK 4 /* Test for read permission. */
- #define W_OK 2 /* Test for write permission. */
- #define X_OK 1 /* Test for execute permission. */
- #define F_OK 0 /* Test for existence. */
- 具体含义如下:
- R_OK 只判断是否有读权限
- W_OK 只判断是否有写权限
- X_OK 判断是否有执行权限
- F_OK 只判断是否存在
- 在宏定义里面分别对应:
- 00 只存在
- 02 写权限
- 04 读权限
- 06 读和写权限
_AccessMode=00表示只判断是否存在
_AccessMode=02表示文件是否可执行
_AccessMode=04表示文件是否可写
_AccessMode=06表示文件是否可读
在windows环境下还可使用函数_access:
- int _access(const char* _Filename, int _AccessMode)
###############
创建新的文件夹:
windows环境下头文件为:
- #include <direct.h>
函数原型为:
- int mkdir(const char *_Path)
_Path:新建文件夹路径,可使用绝对路径,可也用相对路径
windows环境下也可用函数_mkdir:
- _mkdir(const char *_Path)
默认mode是0777,表示最大可能的访问权
Linux环境下头文件为:
- #include <sys/types.h>
- #include <sys/stat.h>
函数原型为:
- int mkdir(const char *pathname, mode_t mode);
S_IRWXU
|
00700权限,代表该文件所有者拥有读,写和执行操作的权限
|
S_IRUSR(S_IREAD)
|
00400权限,代表该文件所有者拥有可读的权限
|
S_IWUSR(S_IWRITE)
|
00200权限,代表该文件所有者拥有可写的权限
|
S_IXUSR(S_IEXEC)
|
00100权限,代表该文件所有者拥有执行的权限
|
S_IRWXG
|
00070权限,代表该文件用户组拥有读,写和执行操作的权限
|
S_IRGRP
|
00040权限,代表该文件用户组拥有可读的权限
|
S_IWGRP
|
00020权限,代表该文件用户组拥有可写的权限
|
S_IXGRP
|
00010权限,代表该文件用户组拥有执行的权限
|
S_IRWXO
|
00007权限,代表其他用户拥有读,写和执行操作的权限
|
S_IROTH
|
00004权限,代表其他用户拥有可读的权限
|
S_IWOTH
|
00002权限,代表其他用户拥有可写的权限
|
S_IXOTH
|
00001权限,代表其他用户拥有执行的权限
|
#################################################
删除文件夹:
windows环境下头文件:
- #include <direct.h>
linux环境下头文件:
- #include <dirent.h>
函数原型为:
- int rmdir(const char *_Path)
在windows环境下也可使用函数_rmdir
######################
实现程序:
- int main(void)
- {
- string dir="./hello";
- if (access(dir.c_str(), 0) == -1)
- {
- cout<<dir<<" is not existing"<<endl;
- cout<<"now make it"<<endl;
- #ifdef WIN32
- int flag=mkdir(dir.c_str());
- #endif
- #ifdef linux
- int flag=mkdir(dir.c_str(), 0777);
- #endif
- if (flag == 0)
- {
- cout<<"make successfully"<<endl;
- } else {
- cout<<"make errorly"<<endl;
- }
- }
- if (access(dir.c_str(), 0) == 0)
- {
- cout<<dir<<" exists"<<endl;
- cout<<"now delete it"<<endl;
- int flag=rmdir(dir.c_str());
- if (flag == 0)
- {
- cout<<"delete it successfully"<<endl;
- } else {
- cout<<"delete it errorly"<<endl;
- }
- }
- //cout<<"Hello World"<<endl;
- cout<<"end..."<<endl;
- cin.get();
- return 0;
- }
转自:http://blog.csdn.net/u012005313/article/details/50688257