C++ UNIX下C++删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/678325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove file in C++ under UNIX
提问by Todd Gamblin
How do you guys typically delete files on Linux OS? I am thinking of using the unlink
function call, but I wonder if you have a better idea, as the C++ standard has no mention of file deletion operation and it is system dependent.
你们通常如何在 Linux 操作系统上删除文件?我正在考虑使用unlink
函数调用,但我想知道您是否有更好的主意,因为 C++ 标准没有提到文件删除操作,并且它依赖于系统。
回答by Todd Gamblin
Yep -- the C++ standard leaves this stuff up to the OS, so if you're on Linux (or any POSIX system), unlink()
is what you've got.
是的——C++ 标准将这些东西留给操作系统,所以如果你在 Linux(或任何 POSIX 系统)上,unlink()
那就是你所拥有的。
The C standard provides remove()
, which you could try, but keep in mind that its behavior is unspecifiedfor anything other than a 'regular file', so it doesn't really shield you from getting into platform-specific filesystem details (links, etc).
C 标准提供了remove()
,您可以尝试,但请记住,除了“常规文件”之外,它的行为是未指定的,因此它并不能真正阻止您进入特定于平台的文件系统详细信息(链接等) .
If you want something higher-level, more robust, and more portable, check out Boost Filesystem.
如果你想要更高级、更健壮、更便携的东西,请查看Boost Filesystem。
回答by Johannes Schaub - litb
回答by Alnitak
回答by smoofra
unlink is the correct way to do it.
unlink 是正确的方法。
回答by Yoric
Note that recent kernels also offer unlinkat
. This function is faster than unlink
if you have a file descriptor on the directory itself.
请注意,最近的内核还提供unlinkat
. 这个函数比unlink
目录本身有文件描述符要快。