关于filesystem在c++11、14、17中的使用问题

1 在C++11时,该类库定义在std::tr2::sys命名空间

#include<filesystem>
using namespace std;
using namespace std::tr2::sys;

//示例

void RemoveFile()
{
    directory_iterator end, ite("aaa\\");

    for (; ite != end; ++ite)
    {
        auto& thePath = ite->path();
        if (!is_directory(thePath))
        {
            const string& sName = ite->path().filename();
            if (sName == "test.txt")
            {
                remove(thePath);
            }
        }
    }
}

2 上述代码在C++14中编译不过,已经不存在tr2命名空间

filesystem放在std::experimental::filesystem空间

#include <experimental/filesystem>

using namespace std::experimental::filesystem;

但是,在(VS2019,C++14)编译时可能会报错C1189,提示该头文件已被取代,建议使用新的

如果不想根据提示改用C++17,那么我们可以坚持自己的想法,在项目配置中加预处理宏定义即可

_SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING

3 上述代码到了C++17仍能编译通过,但我们可以使用新版本的库文件

#include <filesystem>
using namespace std;
using namespace filesystem;

 
void Test()
{
       string filepath = "aaa\\";
       directory_iterator end, ite(filepath);

       for (; ite != end; ++ite)
       {
              auto& thePath = ite->path();
              //string strFullPath = pp.string() / "\\" / thePath.relative_path().string();

              path fp(std::filesystem::current_path() / thePath);
              bool bl = filesystem::exists(fp);

              if (!is_directory(fp))
              {

                     //const string& sName = thePath.filename().c_str();
                     file_status fst(status(fp));
                     file_type ftype =  fst.type();
                     directory_entry de(fp);
                     perms fpem = fst.permissions();
                     size_t sz = file_size(fp);
                     rename(fp, fp);
                     auto sName = thePath.filename().string();
                     auto duration(last_write_time(fp).time_since_epoch());
                     time_t tt = chrono::duration_cast<chrono::seconds>(duration).count();
                     //remove(path(strFullPath));
              }

       }

 
       path dir(filepath);
       directory_iterator di(dir);

       for (const auto& entry : di)
       {
              cout << entry.path().filename() << endl;
              if (entry.is_directory())
              {
              }
              else if(entry.is_regular_file())
              {
              }
              else
              {
              }
       }


       //深度优先遍历
       recursive_directory_iterator rdi(dir);

       for (const auto& entry : di)
       {
              cout << entry.path() << endl;
       }

 
       create_directories(dir);         //创建中间缺失目录
       create_directory(dir / "dd");  //仅末级目录
       remove(dir / "bbb" / "c.txt"); //删除单个
       remove_all(dir);                          //递归删除所有
       const auto options = copy_options::recursive | copy_options::update_existing;
       copy(dir, dir, options);
}

  • 9
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值