in reply to Design/Style question about writing to a file from different functions
Define "Better".
Solution one has an advantage when you use file locking, because the file can be locked until you are done writing it.
Solution two has no real advantages: You still need to pass a parameter specifying the file name. Use a file handle instead and you have solution one.
Solution two is not needed, and the new behaviour does not break solution one: Open the file in read/write mode and you can modify it while still holding a lock.
Also, rethink your concept: Why do you think you need to modify the file on disk? Modify the way you write the data into the file so it is written in correct order. Think about reducing the complexity of the routines writing the file. Split them into smaller parts that can be called in any order needed. If you have the memory, think about creating an array of lines (or any other useful data structure, like a tree) first, process it as needed, and finally write the array in one run into the file.
Show us the relevant parts of the code, perhaps you are making your own life harder than really needed.
Alexander
|
|---|