C/C++ 判断文件夹是否存在以及创建、删除文件夹 windows以及linux通用

来源:互联网 发布:火星15洲际导弹 知乎 编辑:程序博客网 时间:2024/06/05 16:54

##########################################################


判断文件夹是否存在:

在windows环境下头文件为:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #include <io.h>  

在linux环境下头文件为:
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #include <unistd.h>  

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <pre name="code" class="cpp">int access(const char* _Filename, int _AccessMode)  
上述函数在windows和linux环境下均可使用

该函数功能为确定文件或文件夹的访问权限,如果指定的访问权限有效,则函数返回0,否则返回-1

Filename可以是文件路径,也可以是文件夹路径,可以使用绝对路径或相对路径

_AccessMode表示要验证的文件访问权限,有可读、可写、可执行以及是否存在四种权限,当Filename表示文件夹时仅能查询文件夹是否存在

_AccessMode:

头文件unistd.h中有如下定义:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #define R_OK 4 /* Test for read permission. */  
  2. #define W_OK 2 /* Test for write permission. */  
  3. #define X_OK 1 /* Test for execute permission. */  
  4. #define F_OK 0 /* Test for existence. */  
  5. 具体含义如下:  
  6. R_OK 只判断是否有读权限  
  7. W_OK 只判断是否有写权限  
  8. X_OK 判断是否有执行权限  
  9. F_OK 只判断是否存在  
  10. 在宏定义里面分别对应:  
  11. 00 只存在  
  12. 02 写权限  
  13. 04 读权限  
  14. 06 读和写权限  

_AccessMode=00表示只判断是否存在

_AccessMode=02表示文件是否可执行

_AccessMode=04表示文件是否可写

_AccessMode=06表示文件是否可读


在windows环境下还可使用函数_access:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. int _access(const char* _Filename, int _AccessMode)  



###############


创建新的文件夹:

windows环境下头文件为:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #include <direct.h>  


函数原型为:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. int mkdir(const char *_Path)  
该函数功能为建立一个新的目录,创建成功则返回0,否则返回-1

_Path:新建文件夹路径,可使用绝对路径,可也用相对路径

windows环境下也可用函数_mkdir:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. _mkdir(const char *_Path)  

默认mode是0777,表示最大可能的访问权



Linux环境下头文件为:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #include <sys/types.h>  
  2. #include <sys/stat.h>  

函数原型为:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. int mkdir(const char *pathname, mode_t mode);  
该函数功能为创建一个新的目录,并指定它的执行权限。如果创建成功则返回0,否则,返回-1

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权限,代表其他用户拥有执行的权限
可叠加使用,如0755表示S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH。表示该文件所有者拥有读,写和执行操作权限去,该文件用户组拥有可读,可执行的权限,其他用户拥有可读,可执行的权限。


#################################################


删除文件夹:

windows环境下头文件:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #include <direct.h>  

linux环境下头文件:
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #include <dirent.h>  


函数原型为:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. int rmdir(const char *_Path)  
函数功能是删除参数指定的文件夹,成功返回0,否则返回-1

在windows环境下也可使用函数_rmdir


######################


实现程序:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. int main(void)  
  2. {  
  3.     string dir="./hello";  
  4.     if (access(dir.c_str(), 0) == -1)  
  5.     {  
  6.         cout<<dir<<" is not existing"<<endl;  
  7.         cout<<"now make it"<<endl;  
  8. #ifdef WIN32  
  9.         int flag=mkdir(dir.c_str());   
  10. #endif  
  11. #ifdef linux   
  12.         int flag=mkdir(dir.c_str(), 0777);  
  13. #endif  
  14.         if (flag == 0)  
  15.         {  
  16.             cout<<"make successfully"<<endl;  
  17.         } else {  
  18.             cout<<"make errorly"<<endl;  
  19.         }  
  20.     }  
  21.   
  22.     if (access(dir.c_str(), 0) == 0)  
  23.     {  
  24.         cout<<dir<<" exists"<<endl;  
  25.         cout<<"now delete it"<<endl;  
  26.         int flag=rmdir(dir.c_str());  
  27.         if (flag == 0)  
  28.         {  
  29.             cout<<"delete it successfully"<<endl;  
  30.         } else {  
  31.             cout<<"delete it errorly"<<endl;  
  32.         }  
  33.     }  
  34.   
  35.     //cout<<"Hello World"<<endl;  
  36.   
  37.     cout<<"end..."<<endl;  
  38.     cin.get();  
  39.     return 0;  


转自:http://blog.csdn.net/u012005313/article/details/50688257

0 0
原创粉丝点击