Hi !

Thank everyone who helped me with my other question ! But i got something else to ask...

Let's say that in my C++ Console, the user sets its user name, and in the next time the software runs it says :

"Hello, USERNAME !"

But, Problem : I Don't know how to save this string as a "Setting" Which will run the next time the application runs.

Tried registry, Diden't go well... Any other Simple Method ?

Saving settings simply involves saving details to a file. Normally there are a number of variables you require, say name, age, etc. So I save them in a file like so:

USERNAME:twomers
USERAGE:22

Then I read in the file into a std::map<std::string,std::string>. The first parameter is the bit of the line before the : and the second is the bit afterwards. So I'd use it like:

std::cout<< "Hello, " << settings["USERNAME"] << ".";

Edit: I read it in like:

settings[line.substr( 0, colonPosition)] = line.substr(colonPosition+1);

where line is the result of reading the settings file via:

std::getline( inStream, line );

include string and fstream.

Saving settings simply involves saving details to a file. Normally there are a number of variables you require, say name, age, etc. So I save them in a file like so:Then I read in the file into a std::map<std::string,std::string>. The first parameter is the bit of the line before the : and the second is the bit afterwards. So I'd use it like:

std::cout<< "Hello, " << settings["USERNAME"] << ".";

Edit: I read it in like:

settings[line.substr( 0, colonPosition)] = line.substr(colonPosition+1);

where line is the result of reading the settings file via:

std::getline( inStream, line );

include string and fstream.

Well, to which file write ? Can a DLL be ?

No. It's just a text file... say you have a text file called settings.txt which has the details you want in it. Read in the values from that and use them in your program. I do it via something like:

bool readSettings( std::map<std::string,std::string> &settings, const std::string &fname ) {
  std::ifstream in( fname.cstr() );
  if ( !in )
    return false;

  std::string line;
  size_t colonPosition;
  while ( std::getline( in, line ) ) {
    colonPosition = line.find( ":" );
    if ( colonPosition != std::string::npos ) // not found
      settings[line.substr( 0, colonPosition)] = line.substr(colonPosition+1); 
  }

  return true;
}

(note, hasn't been tested. Might have to modify)
And the settings file would be like:

NAME:twomers
AGE:22

Then I'd call it like:

#include <fstream>
#include <string>
#include <map>
#include <iostream>

bool readSettings( std::map<std::string,std::string> &settings, const std::string &fname ) {
  std::ifstream in( fname.cstr() );
  if ( !in )
    return false;

  std::string line;
  size_t colonPosition;
  while ( std::getline( in, line ) ) {
    colonPosition = line.find( ":" );
    if ( colonPosition != std::string::npos ) // not found
      settings[line.substr( 0, colonPosition)] = line.substr(colonPosition+1); 
  }

  return true;
}

int main() {
  std::map<std::string,std::string> settings;
  readSettings( settings, "settings.txt" );
  std::cout<< settings["NAME"] << " is " << settings["AGE"] << " years old\n";

  return 0;
}

Note though that this doesn't validate the existence of these elements in the map. If you don't know a map is something which correlates one variable to another, here one string to another. The first describes the second in a unique way.

I like the concept. You could even pre-initialize the map in memory with default values before attempting to read the file. If the configuration was in the file it would be overwritten.

I do have a couple of questions though:

Where does the settings.txt file end up?.

In order to not conflict with other programs using the same design (and filename) the storage needs to be program specific (or the name does).

If the file contains user specific data and the computer has multiple users, the storage needs to be user specific or the application needs to take the logged-in user into consideration.

If the file is in the program's directory, it meets the program specific requirement, but you might need admin privileges to run it under some versions of windows.

>> You could even pre-initialize the map in memory with default values before attempting to read the file.
Yeah. I set the values of the map members to "" before reading the values. So I don't have to check they exist. I also have something which specifies whether the variables are necessary or optional and call a verification function before proceeding.

>> Where does the settings.txt file end up?.
Same directory as the executable. You can write a function to save the elements too using iterators and the like.

This article has been dead for over six months. Start a new discussion instead.