Removing a non empty directory programmatically in C or C++

后端 未结 8 2368
一生所求
一生所求 2020-11-27 16:50

How to delete a non empty directory in C or C++? Is there any function? rmdir only deletes empty directory. Please provide a way without using any external library.

8条回答
  •  有刺的猬
    2020-11-27 17:40

    C++17 has which is based on the boost version.

    Use std::experimental::filesystem::remove_all to remove recursively.

    If you need more control, try std::experimental::filesystem::recursive_directory_iterator.

    You can also write your own recursion with the non-resursive version of the iterator.

    namespace fs = std::experimental::filesystem;
    void IterateRecursively(fs::path path)
    {
      if (fs::is_directory(path))
      {
        for (auto & child : fs::directory_iterator(path))
          IterateRecursively(child.path());
      }
    
      std::cout << path << std::endl;
    }
    

提交回复
热议问题