在C中强制退出时释放所有打开的文件和目录

发布于 2024-08-21 22:55:00 字数 382 浏览 4 评论 0原文

我想强制释放我的程序在执行期间打开的所有打开的文件和目录。我想这样做是因为我有一个非常大的程序,它打开了许多我无法跟踪的文件和目录。有什么办法可以做到吗?意味着我想检索所有打开的文件和目录的列表并在退出时关闭它们。

我知道使用 atexit() 函数注册退出处理程序。可以用它来做点什么吗?

编辑: 我在windows上有cygwin。我想做上述事情,因为我的程序的资源没有被自动释放。我有一个创建的目录,然后使用 opendir() 打开。我的程序完成后,当我尝试删除该目录时,它显示“无法删除,正在被另一个程序使用”。但是当我终止 explorer.exe 并再次重新启动时,只有我才能删除该目录。

问题是它的发生不均匀。我可以删除某些目录,但无法删除某些目录。

I want to release all open files and directories forecefully my program has opened during its execution. I want to do this because I have a very big program and it opens many files and directories which I am not able to keep track. Is there any way to do it? means I want to retrieve a list of all open files and directories and close them at exit.

I know registration of exit handlers using atexit() function. Can something be done with it?

Edit:
I have cygwin on windows. I want to do the above thing because my program's resources are not being automatically released. I have a directory which is created and then opened using opendir(). After my program finishes, when I try to delete that directory, it says "cant delete, being used by another program". But when I terminate explorer.exe and again restart, then only I am able to delete that directory.

The problem is it is happening unevenly. I am able to delete some directories and not able to delete some.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

韬韬不绝 2024-08-28 22:55:00

如果“释放”的意思是“关闭”,那么无论如何都会发生。 C 运行时和操作系统会为您处理这些事情。进程终止时,进程打开的所有资源都将被关闭。如果没有这样做,那将是一个非常罕见(且质量很差)的环境。

If by "release" you mean "close", that will happen anyway. The C runtime and the operating system will take care of that for you. On process termination all resources the process had open will be closed off. It would be a very rare (and poor quality) environment that didn't do that.

明天过后 2024-08-28 22:55:00

man signal 应该会给你一些提示。这捕获了大部分终止信号,而不仅仅是正常的信号。

man signal should give you some hints. This catches most of termination signals, not only normal ones.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文