问题
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