如何删除(POSIX)C中的目录及其内容? [英] How to delete a directory and its contents in (POSIX) C?

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

问题描述

我对非递归情况最感兴趣,但我猜可能会跟踪此问题的其他人更愿意看到递归情况.

I am most interested in the non-recursive case, but I am guessing others who might track this question would prefer seeing the recursive case.

基本上,我们的目标是:

Basically, we are aiming to accomplish:

rm -rf <target>

然而,系统调用将是一个不成熟的答案.

However, a system call would be an immature answer.

推荐答案

  1. 您需要使用nftw()(或可能 ftw()) 遍历层次结构.
  2. 您需要使用unlink() 删除文件和其他非目录.
  3. 您需要使用rmdir() 删除(空)目录.

最好使用 nftw()(而不是 ftw()),因为它为您提供了诸如 FTW_DEPTH 之类的控件以确保在访问目录本身之前访问目录下的所有文件.

You would be better off using nftw() (rather than ftw()) since it gives you controls such as FTW_DEPTH to ensure that all files under a directory are visited before the directory itself is visited.

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

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