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

查看:143
本文介绍了(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() 删除(空)目录。

  1. You need to use nftw() (or possibly ftw()) to traverse the hierarchy.
  2. You need to use unlink() to remove files and other non-directories.
  3. You need to use rmdir() to remove (empty) directories.

你最好使用 nftw()(而不是 ftw())因为它为您提供诸如 FTW_DEPTH 的控制权e,在目录本身被访问之前,目录下的所有文件都被访问。

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天全站免登陆