如何删除(POSIX)C目录及其内容? [英] How to delete a directory and its contents in (POSIX) C?
本文介绍了如何删除(POSIX)C目录及其内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我最感兴趣的非递归的情况下,但我猜测其他人谁可能会跟踪这个问题将preFER看到递归情况。
基本上,我们的目标是要做到:
室射频<目标>
然而,一个系统调用是一个不成熟的答案。
解决方案
- 您需要使用
nftw()
(或可能FTW()
一>)遍历层次。 - 您需要使用<一个href=\"http://pubs.opengroup.org/onlinepubs/9699919799/functions/unlink.html\"><$c$c>unlink()$c$c>删除文件和其他非目录。
- 您需要使用
命令rmdir()
删除(空)目录。
您会过得更好使用 nftw()
(而不是 FTW()
),因为它可以让你控制如 FTW_DEPTH
来确保一个目录下的所有文件都被访问自己的目录访问过。
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.
解决方案
- You need to use
nftw()
(or possiblyftw()
) to traverse the hierarchy. - You need to use
unlink()
to remove files and other non-directories. - You need to use
rmdir()
to remove (empty) directories.
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屋!
查看全文