如果我不叫FCLOSE()在C程序会发生什么? [英] What happens if I don't call fclose() in a C program?

查看:155
本文介绍了如果我不叫FCLOSE()在C程序会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道,打开一个文件,fopen()函数,而不是关闭它是可怕的不负责任,和不好的形式。这只是纯粹的好奇心,所以请我的幽默:)

Firstly, I'm aware that opening a file with fopen() and not closing it is horribly irresponsible, and bad form. This is just sheer curiosity, so please humour me :)

我知道,如果一个C程序打开一堆文件,并永远不会关闭任何人,最终fopen()函数将启动失败。是否还有其他的副作用,可能导致code本身之外的问题?举例来说,如果我有打开一个文件,然后退出而不将其关闭程序时,可能会导致运行该程序的人有问题?将这样的程序泄露任何东西(内存,文件句柄)?莫不是再次访问该文件的问题一旦程序已经完成?如果程序连续多次运行会发生什么?

I know that if a C program opens a bunch of files and never closes any of them, eventually fopen() will start failing. Are there any other side effects that could cause problems outside the code itself? For instance, if I have a program that opens one file, and then exits without closing it, could that cause a problem for the person running the program? Would such a program leak anything (memory, file handles)? Could there be problems accessing that file again once the program had finished? What would happen if the program was run many times in succession?

推荐答案

只要你的程序正在运行,如果你继续打开文件,而无需关闭它们,最有可能的结果是,你会用完文件描述符/句柄可用为你的过程,并试图打开多个文件最终将失败。在Windows中,这也可以prevent其他进程打开或删除文件,你有开放的,因为在默认情况下,文件被独占共享模式是prevents其它进程打开它们打开。

As long as your program is running, if you keep opening files without closing them, the most likely result is that you will run out of file descriptors/handles available for your process, and attempting to open more files will fail eventually. On Windows, this can also prevent other processes from opening or deleting the files you have open, since by default, files are opened in an exclusive sharing mode that prevents other processes from opening them.

一旦你的程序退出时,操作系统会在后面进行清理。它会关闭你离开打开的文件,当它结束你的进程,并执行任何其他清理是必要的(例如,如果一个文件被标记为删除-上接近,它会删除该文件,那么,请注意之类的事情是平台特异性)。

Once your program exits, the operating system will clean up after you. It will close any files you left open when it terminates your process, and perform any other cleanup that is necessary (e.g. if a file was marked delete-on-close, it will delete the file then; note that that sort of thing is platform-specific).

然而,另一个问题要小心为缓冲数据的。大多数文件写出来磁盘之前流在内存缓冲区中的数据。如果您使用 FILE * 从stdio库流,则有两种可能性:

However, another issue to be careful of is buffered data. Most file streams buffer data in memory before writing it out to disk. If you're using FILE* streams from the stdio library, then there are two possibilities:


  1. 您的程序正常退出,通过调用 退出(3) 功能,或者从(其中隐式调用退出(3))。
  2. 您的程序异常退出;这可以通过调用 中止(3) 或< A HREF =htt​​p://linux.die.net/man/3/_exit> _Exit(3) ,从信号/异常奄奄一息,等等。

  1. Your program exited normally, either by calling the exit(3) function, or by returning from main (which implicitly calls exit(3)).
  2. Your program exited abnormally; this can be via calling abort(3) or _Exit(3), dying from a signal/exception, etc.

如果你的程序正常退出,C运行时将采取冲水那个曾经打开任何缓冲的流照顾。所以,如果你有缓冲写入数据文件* 这是不会被刷新,这将是正常的退出刷新。

If your program exited normally, the C runtime will take care of flushing any buffered streams that were open. So, if you had buffered data written to a FILE* that wasn't flushed, it will be flushed on normal exit.

相反,如果你的程序异常退出,任何缓冲的数据的的被刷新。操作系统只是说:哦,亲爱的我,你留下的文件描述符,我更好地贴近了你进程终止时;它不知道有一些随机数据在内存中的某处躺在了预期的程序写入到磁盘上,但没有。所以一定要小心这一点。

Conversely, if your program exited abnormally, any buffered data will not be flushed. The OS just says "oh dear me, you left a file descriptor open, I better close that for you" when the process terminates; it has no idea there's some random data lying somewhere in memory that the program intended to write to disk but did not. So be careful about that.

这篇关于如果我不叫FCLOSE()在C程序会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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