0

I have a question on easy practices to read a settings.dat file in a c++ software.

In my main c++ program, I declare global variables, read settings.dat file for settings of my run and then do various things on many data files.

/************************************/
/** now program.cpp *****************/
/************************************/

int show-warnings;
int indent-spaces;

// global variables

void readSettings()
{
    ifstream inputFile ("settings.dat");
    inputFile >> show-warnings;
    inputFile >> indent-spaces;
    inputFile.close();
}

int main(int argc, char *argv[])
{
    readSettings();
    // do various things
    return 0;
}
/************************************/
/** now: settings.dat ***************/
/************************************/
3 
1
2

These settings are not labelled. Every time I change the settings, it's more difficult for me to remember what order these things are for.

This is what I'd like:

/************************************/
/** i'd like settings.dat ***********/
/************************************/
indent-spaces: 3
show-warnings: yes
purpose: update

I'm trying to make my settings.dat file more meaningful by (1) adding what each parameter is, followed by a colon, and (2) change to not-just-integers argument

If I have 20 parameters, and not all arguments are specified each time in the data file, I'd like to assume default values.

Could somebody point me to an easy/robust way of managing variables for these parameters within my c++ software? I don't even know what terms to search for in Google.

By the way, I use Windows 7, MING++, direct compilation.

schlebe
  • 3,387
  • 5
  • 37
  • 50
  • 2
    Easiest thing would be to use a prexisting format, and a library that handles it, such as JSON by nlohmann [github link](https://github.com/nlohmann/json). This one in particular can be integrated by just copying the json.hpp file, sticking it in your project, and #including it in any file that needs it. – JohnFilleau Mar 30 '20 at 01:08
  • You will have to write C++ code to do this kind of parsing. Perhaps rethink completely the design of your program, and store configuration settings in a `std::unordered_map`, with each setting identified by its text string, so reading and parsing it becomes laughably trivial, and retrieving a particular configuraiton setting is done by its name. Not a particularly complicated operation, but it does require some C++ knowledge, which someone who is not a programmer, by trade, may not grasp. That's just the way it is. – Sam Varshavchik Mar 30 '20 at 01:11
  • Search for an INI parser, or TOML parser. These formats are handy. – numzero Mar 30 '20 at 01:14
  • 1
    You expected file format looks like YAML to me. Try https://github.com/jimmiebergmann/mini-yaml It is just one header file so there won't be trouble building or linking. – Jerry Jeremiah Mar 30 '20 at 01:54
  • 1
    C++ is very difficult to learn. It is not convenient for scripting. If you need only scripting and automation then try Python. Python is suitable for beginners. It is studied in schools as the first programming language. Also, it is convenient for scripting. – Andrey Epifantsev Mar 30 '20 at 02:06

1 Answers1

0

Your settings.dat looks like INI-file. There are lots of methods and libraries for reading INI-files. You can read about them here.

Andrey Epifantsev
  • 976
  • 1
  • 11
  • 27