静态库封装之ComDir类

打印 上一主题 下一主题

主题 1016|帖子 1016|积分 3048

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
ComDir.h
  1. /*
  2. @author:EricsT
  3. @data:20241031
  4. @version:V1.0
  5. @history:
  6.         @author @data @version @content
  7.         EricsT 20241031 V1.0 新增ComDir类[判断存在性以及创建目录]
  8. */
  9. #pragma once
  10. #include <string>
  11. #include <fstream>
  12. using namespace std;
  13. class ComDir
  14. {
  15. public:
  16.         /*
  17.         func:判断文件夹是否存在
  18.         @strDirPath:文件夹路径
  19.         @csDirPath:文件夹路径
  20.         @pchDirPath:文件夹路径
  21.         ret:存在true,不存在false
  22.         */
  23.         static bool isDirExists(string strDirPath);
  24.         static bool isDirExists(CString csDirPath);
  25.         static bool isDirExists(PCHAR pchDirPath);
  26.         /*
  27.         func:创建文件夹
  28.         @strDirPath:文件夹路径
  29.         @csDirPath:文件夹路径
  30.         @pchDirPath:文件夹路径
  31.         ret:创建成功true,创建失败false
  32.         */
  33.         static bool creatDir(string strDirPath);
  34.         static bool creatDir(CString csDirPath);
  35.         static bool creatDir(PCHAR pchDirPath);
  36. };
复制代码
ComDir.cpp
  1. /*
  2. @author:EricsT
  3. @data:20241031
  4. @version:V1.0
  5. */
  6. #include "stdafx.h"
  7. #include "ComDir.h"
  8. #include <io.h>
  9. #include <sys/stat.h>
  10. #include <direct.h>
  11. bool ComDir::isDirExists(string strDirPath)
  12. {
  13.         if (!access(strDirPath.c_str(), 0))//判断路径是否存在
  14.                 return false;
  15.         struct stat status;
  16.         stat(strDirPath.c_str(), &status);//获取状态变量
  17.         if (status.st_mode & S_IFDIR)//取模式判断是否是目录
  18.                 return true;
  19.         return false;
  20. }
  21. bool ComDir::isDirExists(CString csDirPath)
  22. {
  23.         //CStringToString
  24.         int len = csDirPath.GetLength();
  25.         PCHAR pch = new char[len + 1];
  26.         size_t pchSize = wcstombs(pch, csDirPath, len + 1);
  27.         if (pchSize == wstring::npos)
  28.         {
  29.                 delete pch;
  30.                 return "";
  31.         }
  32.         string strDirPath(pch);
  33.         delete pch;
  34.         if (!access(strDirPath.c_str(), 0))
  35.                 return false;
  36.         struct stat status;
  37.         stat(strDirPath.c_str(), &status);
  38.         if (status.st_mode & S_IFDIR)
  39.                 return true;
  40.         return false;
  41. }
  42. bool ComDir::isDirExists(PCHAR pchDirPath)
  43. {
  44.         if (!access(pchDirPath, 0))
  45.                 return false;
  46.         struct stat status;
  47.         stat(pchDirPath, &status);
  48.         if (status.st_mode & S_IFDIR)
  49.                 return true;
  50.         return false;
  51. }
  52. bool ComDir::creatDir(string strDirPath)
  53. {
  54.         string strCreate = strDirPath;
  55.         string strCoplete = strDirPath.substr(0, 2);//取系统盘
  56.         strCreate = strCreate.substr(3);//除去系统盘之后的路径
  57.         while (true)
  58.         {
  59.                 size_t iPos = strCreate.find('\\');
  60.                 strCoplete += '\\' + strCreate.substr(0, iPos);//按层级取目录
  61.                 if (-1 == _access(strCoplete.c_str(), 0)) //判断该目录是否存在
  62.                 {
  63.                         if (0 != _mkdir(strCoplete.c_str()))//不存在就创建目录
  64.                                 return false;
  65.                 }
  66.                 if (strCreate.npos == iPos)//最后一级目录
  67.                         break;
  68.                 strCreate = strCreate.substr(iPos + 1);
  69.         }
  70.         return true;
  71. }
  72. bool ComDir::creatDir(CString csDirPath)
  73. {
  74.         //CStringToString
  75.         int len = csDirPath.GetLength();
  76.         PCHAR pch = new char[len + 1];
  77.         size_t pchSize = wcstombs(pch, csDirPath, len + 1);
  78.         if (pchSize == wstring::npos)
  79.         {
  80.                 delete pch;
  81.                 return "";
  82.         }
  83.         string strDirPath(pch);
  84.         delete pch;
  85.         string strCreate = strDirPath;
  86.         string strCoplete = strDirPath.substr(0, 2);
  87.         strCreate = strCreate.substr(3);
  88.         while (true)
  89.         {
  90.                 size_t iPos = strCreate.find('\\');
  91.                 strCoplete += '\\' + strCreate.substr(0, iPos);
  92.                 if (-1 == _access(strCoplete.c_str(), 0))
  93.                 {
  94.                         if (0 != _mkdir(strCoplete.c_str()))
  95.                                 return false;
  96.                 }
  97.                 if (strCreate.npos == iPos)
  98.                         break;
  99.                 strCreate = strCreate.substr(iPos + 1);
  100.         }
  101.         return true;
  102. }
  103. bool ComDir::creatDir(PCHAR pchDirPath)
  104. {
  105.         string strDirPath(pchDirPath);
  106.         string strCreate = strDirPath;
  107.         string strCoplete = strDirPath.substr(0, 2);
  108.         strCreate = strCreate.substr(3);
  109.         while (true)
  110.         {
  111.                 size_t iPos = strCreate.find('\\');
  112.                 strCoplete += '\\' + strCreate.substr(0, iPos);
  113.                 if (-1 == _access(strCoplete.c_str(), 0))
  114.                 {
  115.                         if (0 != _mkdir(strCoplete.c_str()))
  116.                                 return false;
  117.                 }
  118.                 if (strCreate.npos == iPos)
  119.                         break;
  120.                 strCreate = strCreate.substr(iPos + 1);
  121.         }
  122.         return true;
  123. }
复制代码
在VS编译器内会报C4996错误,解决见下文:
C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. - EricsT - 博客园 (cnblogs.com)

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

熊熊出没

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表