最佳实践:文件打开/关闭操作 [英] best practice: file open/close operations

查看:142
本文介绍了最佳实践:文件打开/关闭操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以:我们正在研究教科书叫什么高级文件操作,我写了一个很好的小库存程序。简单的问题:什么是更有效/被认为最好:打开主文件,并通过引用各种功能(一个添加,读取和更改),或让每个函数打开和关闭文件需要?任何真正的优势,无论是方式?

So: we're studying what the textbook calls "advanced file operations", and I'm writing a nice little inventory program. Simple question: what's more efficient/considered best: to open the file in main and send it by reference to the various functions (one each for adding to, reading from, and altering), or to let each function open and close the file as needed? Any real advantage either way?

推荐答案

在我的个人经验和意见更清洁只是使文件阅读器功能,

in my personal experiences and opinions its cleaner to just make a file reader function that has the filename passed in, and another one for writing.

如果你在main()中执行函数,如果你处理多个文件,它会变得非常快。

if you did the functions in main() it would get ugly really fast if you process multiple files.

这篇关于最佳实践:文件打开/关闭操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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