如何在 C++ 中关闭文件时删除该文件在 Linux 上?
我希望只有在关闭文件时才从磁盘中删除文件。直到那时,其他进程应该能够看到磁盘上的文件并读取其内容,但最终在文件关闭后,它应该从磁盘中删除,并且在磁盘上对其他进程不再可见。
I wish for a file to be deleted from disk only when it is closed. Up until that point, other processes should be able to see the file on disk and read its contents, but eventually after the close of the file, it should be deleted from disk and no longer visible on disk to other processes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
打开文件,然后在打开时将其删除。其他进程将能够使用该文件,但一旦文件的所有句柄都关闭,该文件就会被删除。
编辑:根据 WilliamKF 后来添加的评论,这不会实现他想要的 - 它会保留文件本身,直到它的所有句柄都关闭,但是文件的目录条目一旦您调用
unlink
/remove
,名称就会消失。Open the file, then delete it while it's open. Other processes will be able to use the file, but as soon as all handles to file are closed, it will be deleted.
Edit: based on the comments WilliamKF added later, this won't accomplish what he wants -- it'll keep the file itself around until all handles to it are closed, but the directory entry for the file name will disappear as soon as you call
unlink
/remove
.Unix 中打开的文件是引用计数的。每次
open(2)
都会递增计数器,每次close(2)
都会递减计数器。该计数器由系统上的所有进程共享。然后是磁盘文件的链接计数。全新文件的计数为一。计数由
link(2)
系统递增称呼。unlink(2)
减少它。当此计数降至零时,文件将从文件系统中删除。完成您要求的唯一方法是在一个进程中打开文件,然后
unlink(2)
它。其他进程将能够在open(2)
和unlink之间
。假设该文件只有一个链接,当所有打开该文件的进程关闭它时,该链接就会被删除。open(2)
或stat(2)
它 (2)Open files in Unix are reference-counted. Every
open(2)
increments the counter, everyclose(2)
decrements it. The counter is shared by all processes on the system.Then there's a link count for a disk file. Brand-new file gets a count of one. The count is incremented by the
link(2)
system call. Theunlink(2)
decrements it. File is removed from the file system when this count drops to zero.The only way to accomplish what you ask is to open the file in one process, then
unlink(2)
it. Other processes will be able toopen(2)
orstat(2)
it betweenopen(2)
andunlink(2)
. Assuming the file had only one link, it'll be removed when all processes that have it open close it.使用 取消链接
Use unlink
不确定,但你可以尝试删除,但它看起来更像c -风格。
也许boost::filesystem::remove?
您可以创建一个对引用进行计数的包装类,使用上述方法之一来删除 de file 。
Not sure, but you could try remove, but it looks more like c-style.
Maybe boost::filesystem::remove?
You could create a wrapper class that counts references, using one of the above methods to delete de file .
我认为您需要将“关闭文件”的概念扩展到 fclose 或 std::fstream::close 之外的任何您打算做的事情。这可能很简单,
也可能更复杂。据我所知,它甚至可能要简单得多 - 如果您仅在代码中的一两个位置关闭文件,最好的办法可能是简单地
取消链接
那里的文件(或使用 boost ::filesystem::remove,正如汤姆建议的那样)。OTOH,如果您想要实现的是从您的进程启动的进程可以使用该文件,您可能根本不需要将其保留在磁盘上。
分叉
进程继承打开的文件。不要忘记重复
它们,以免子级中的查找影响父级中的位置,反之亦然。I think you need to extend your notion of “closing the file” beyond
fclose
orstd::fstream::close
to whatever you intend to do. That might be as simple asor it may be something much more elaborate. For all I know, it may even be much simpler – if you close files only at one or two places in your code, the best thing to do may be to simply
unlink
the file there (or use boost::filesystem::remove, as Tom suggests).OTOH, if all you want to achieve is that processes started from your process can use the file, you may not need to keep it lying around on disk at all.
fork
ed processes inherit open files. Don't forget todup
them, lest seeking in the child influences the position in the parent or vice versa.