如何删除C ++中的非空目录? [英] How to remove a non-empty directory in C++?

查看:117
本文介绍了如何删除C ++中的非空目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,如何删除包含其所有文件的目录?我知道有 rmdir ,但它只会删除非空目录,所以如何列出并删除所有包含的文件?

我知道使用Boost文件系统并不困难,但我希望避免构建并依赖于这个小任务... 是的,你通常必须先删除内容。如果你不想使用Boost,那么你很难用非可移植的代码来查找所有的文件(例如, FindFirstFile ,<$ c在Windows上是$ c> FindNextFile ,在Unix上是 opendir readdir 并删除所有这些。


In C++, how can I remove a directory with all its contained files? I know there is rmdir, but it will only remove non-empty directories, so how do I list and remove all contained files first?

I know it shouldn't be hard using Boost Filesystem, but I kind of want to avoid building and depending on it just for this one little task ...

解决方案

Yes, you normally have to remove the contents first. If you don't want to use Boost for this, you're pretty much stuck with writing non-portable code to find all the files (e.g., FindFirstFile, FindNextFile on Windows, opendir, readdir on Unix and similar) recursively, and remove all of them.

这篇关于如何删除C ++中的非空目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆