views:

151

answers:

5

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?

A: 

If you insist to do it manually, I suggest JSON instead of XML.

Also, consider sqlite.

hasen j
+1  A: 

Could you please enlighten us why don't you want to use a database engine for it?

If it is just for learning then.... give us please an estimated size of stored data in that file and the access pattern (how many users, how often they do it etc.)?

The challenge will be to create an efficient search and modification code.

For the search, it's about data structures and organization. For the modification, it's how would you write updates to the file without reading it completely into memory, updating it there and then writing it again completely back to the file.

User
+4  A: 

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.

Vilx-
A: 

This sounds like a perfect job for SQLite. Small, fast, flexible and easy to use.

snemarch
+1  A: 

If this is a project that will actually be used, with the potential to have features added over time, go for a database solution from the start, even if it seems overkill. I've been down this road before, small features get added over time, and before you realize it you have implemented a database. Poorly. Bite the bullet and use a database.

If this is a learning exercise, it depends on the amount of data you want to store. If it is small, the easiest thing to do is read the entire file into memory and operate on it there. When changes are made, write the entire file back out to disk. If the data is too large to do that, the next best thing is to have fixed sized records. Create a POD struct that contains all of the data (i.e., no pointers, stl containers, etc). Then you can rewrite individual records without needed to rewrite the entire file. If neither of these will work, your best bet is a database solution.

KeithB