Search code examples
c++xmlfilesaveinventory

What is the best way I should go about creating a program to store information into a file, edit the information in that file, and add new information


I'm about to start on a little project i'm trying to do where I create a C++ program to store inventory data into a file ( I guess a .txt will do )

  • • Item Description • Quantity on Hand • Wholesale Cost • Retail Cost • Date Added to Inventory

I need to be able to:

• Add new records to the file • Display any record in the file • Change any record in the file

Is there anything I should know of before I start this that could make this much more easy & efficient...

Like for example, should I try and use XML or what that be too hard to work with via C++?

I've never really understood the most efficient way of doing this.

Like would I search through the file and look for things in brackets or something?

EDIT

The datasize shouldn't be too large. It is for homework I guess you could say. I want to write the struct's contents into a file's route, how would I go about doing that?


Solution

  • There are many approaches. Is this for homework or for real use? If it's for homework, there are probably some restrictions on what you may use.

    Otherwise I suggest some embedded DBMS like SQLite. There are others too, but this will be the most powerful solution, and will also have the easiest implementation.

    XML is also acceptable, and has many reusable implementations available, but it will start loosing performance once you go into thousands of records. The same goes for JSON. And one might still debat which one is simpler - JSON or XML.

    Another possibility is to create a struct and write its contents directly to the file. Will get tricky though if the record size is not constant. And, if the record format changes, the file will need to be rebuilt. Otherwise this solution could be one of the best performance-wise - if implemented carefully.