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

后端 未结 8 2392
一生所求
一生所求 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 <experimental\filesystem> 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;
    }
    
    0 讨论(0)
  • 2020-11-27 17:42

    You can use opendir and readdir to read directory entries and unlink to delete them.

    0 讨论(0)
提交回复
热议问题