昨天写了个文件处理函数,漏写了fclose,结果今天有个朋友指出了,我发现编译器没报错,顺便查了一下资料,发现fclose可以在程序的正常结束时由函数自己自动调用,linux下是这样,不知windows下是否也是这样,总之自己写上这个函数是最好的习惯
<<THE C PROGRAMMING LANGUAGE>>
看看上面是怎么对fclose进行说明的
在7章第5节的最后,有段话:
The function
int fclose(FILE *fp)
is the inverse of fopen, it breaks the connection between the file pointer and the external name that was established by fopen, freeing the file pointer for another file. Since most operating systems have some limit on the number of files that a program may have open simultaneously, it's a good idea to free the file pointers when they are no longer needed, as we did in cat. There is also another reason for fclose on an output file - it flushes the buffer in which putc is collecting output. fclose is called automatically for each open file when a program terminates normally. (You can close stdin and stdout if they are not needed. They can also be reassigned by the library function freopen.)
翻译如下:
fclose是fopen相反的操作,它打断了fopen建立起来的文件指针和外部名字(文件名)之间的连接,释放了文件指针.由于大部分的操作系统对文 件打开的数目都有最大限制,因此不用的文件指针最好还是释放掉.fclose还有其他的作用,那就是刷新缓冲.程序自然结束会自动调用fclose.(你 可以使用使用close关闭标准输入和标准输出.还有另外一个库函数freopen可以进行重定向)