best practice: file open/close operations [closed]

你。 提交于 2020-01-16 22:02:18

问题


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?


回答1:


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.

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



来源:https://stackoverflow.com/questions/15950267/best-practice-file-open-close-operations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!