I’m a beginner in C++ programming, so please forgive me if this question is somewhat absurd. I am just wondering which approach is better:

1. Upon program start-up, create an array of objects based on data stored in a text file. When the program terminates, the array of objects (and any modifications to it) are written back into the text file.
OR
2. Whenever an object needs to be modified, only that particular object is recreated from the text file, and is written back into the file after modification.

I understand that the 1st approach will consume much more memory than the 2nd, especially when the program is required to work with thousands of objects. However:
• With the 2nd approach, is it safe to open and close the file every time a transaction is made?
• If the 2nd approach is better, then when linked-lists, vectors, etc. should be used?
• Are there any advantages / disadvantages to adopting the 1st or 2nd approach?

The 1st approach will be easier and probably faster as well, but it will use more memory at any given time. Using more memory means the chance of thrashing is increased, and the performance will drop a lot. The 2nd approach will be trickier to implement and maintain, and will probably be slower due to always accessing the file, but you'll save memory if the file is large.

> With the 2nd approach, is it safe to open and close the file every time a transaction is made?
No less safe than it would be to open the file once when the program starts. You have to worry more about other processes locking the file and less about changes to the file after you've loaded records.

> If the 2nd approach is better, then when linked-lists, vectors, etc. should be used?
If you choose the 2nd approach--better depends on your needs and preferences--then you won't be using any kind of list at all. It sounds like the 2nd approach only loads on object at a time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.