关于linux下fclose需不需要的问题


昨天写了个文件处理函数,漏写了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可以进行重定向)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值