std :: ofstream :: close()实际上做了什么? [英] What does std::ofstream::close() actually do?

查看:1134
本文介绍了std :: ofstream :: close()实际上做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题:如何防止日志从应用程序崩溃?引导我到另一个 - std :: ofstream :: close()实际上做什么?我知道它调用 flush()这是一回事。但还有什么?



编辑:
让我重新整理我的问题 - 是对实际文件进行的实际操作调用 close()还是只是 std :: ofstream 内部清理东西?

$ b除了刷新用户空间缓冲区,即 flush()

close(2)在底层文件描述符上调用。这取决于操作系统发生了什么,但很可能没有什么影响文件占用的实际存储。



会发生什么 (如果文件描述符是该进程中该文件的最后一个引用)与该文件相关联的文件条目将从进程的打开文件表中删除。也就是说释放与进程相关的内核内存。


This question: How to protect log from application crash? has lead me to another - what does std::ofstream::close() actually do? I know it calls flush() and that's one thing. But what else? What closing the file actually is?

Edit: Let me rephrase my question - is anything physically done to the actual file during the call to close() or is it just std::ofstream internal cleanup stuff?

解决方案

Other than flushing the userspace buffers, i.e. flush(), close(2) is called on the underlying file-descriptor. It depends on the operating system what happens then, but most likely nothing happens to the actual storage occupied by the file.

What will happen is that the (if the file descriptor was the last reference in that process to that file) file entry associated with the file gets removed from the open-file table of the process. I.e. freeing process-associated kernel-memory.

这篇关于std :: ofstream :: close()实际上做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆