views:

90

answers:

3

Basically, I want to have my program retrieve various variables from the hard drive disk for use in the program. I guess, before asking for details, I should verify that is even possible. Is it?

If it is, what is the best way? I've seen the use of .ini files, but I don't know how to retrieve data specifically from .ini files. Is it the same as a text file that you have messed with to make sure variables will always be on x line? For instance:

ifstream variables("variables.ini");

//some code later
//assume line 'x' holds a variable

if(variables.good())
{
   variables.get(x);
}

//or whatever =/

I'm kind of at a loss for what to do here.

Edit: my language of choice is C++.

A: 

You decide the format of the .ini file of your application. I usually work with XML because then you can organize your information by scope and there is already a bunch of libs to handle storing and retrieving information from XML trees.

Edit: for C++ - http://xerces.apache.org/xerces-c/

freitass
A: 

If you're going to be pulling values from the files frequently, and especially if you need to update(persist) those values, a database is a great option. It also will give you good performance as your dataset grows.

Kekoa
+1  A: 

The first questions you need to address are the who and the why. Your options on the how will follow from those.

So who (or what) will be accessing the data? If it is just the program itself then you can store the data however you want - binary file, xml, database, ini file, whatever. However if the data needs to be easily accessible to the user so they can change it prior to a run then a text file like an ini, which can be easily edited, makes sense. In order to edit the data stored in other formats you may have to write an entirely separate program just to manipulate the stored parameters. Maybe that makes sense in your situation or maybe not but it will be more work.

If you choose to go the ini route then you are on the right track. They are just text files. A common format is to have sections (usually in brackets), and then key/value pairs within the sections. Usually comment lines start with semicolon, a nice touch for the users who may want to flip back and forth between settings.

So something like this:

[System]
    datapath = /home/me/data

[Application]
    start_count = 12
;   start_count = 20  //this is a comment

You don't have to worry about specific lines for your data. You just read through the file line by line. Empty or comment lines get tossed. You take note of what section you are in and you process the key/value pairs.

There are many ways to store the parsed file in your program. A simple one may be to concatenate the section name and key into a key for a map. The value (of the key/value pair) would be the data for the map.

So "Systemdatapath" could be one map key and its value would be "/home/me/data". When your program needs to use the values it just looks it up by key.

That's the basics. Ultimately you will want to embellish it. For instance, methods to retrieve values by type. E.g. getString(), getInteger(), getFloat(), etc.

Duck
Great, exactly what I needed to know. I think I will keep it with .ini, if only for simplicity's sake. I just wanted to make sure that as long as I parsed it my way and accessed it my way, it wouldn't matter if there is a convention in place (or whatever). +1 and selected answer
Hooked