943,870 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 588
  • C++ RSS
Dec 30th, 2008
0

Settings in Application (C++)

Expand Post »
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 ?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ronen444 is offline Offline
18 posts
since Dec 2008
Dec 30th, 2008
0

Re: Settings in Application (C++)

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:
Quote ...
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:
C++ Syntax (Toggle Plain Text)
  1. std::cout<< "Hello, " << settings["USERNAME"] << ".";
Edit: I read it in like:
cpp Syntax (Toggle Plain Text)
  1. settings[line.substr( 0, colonPosition)] = line.substr(colonPosition+1);
where line is the result of reading the settings file via:
cpp Syntax (Toggle Plain Text)
  1. std::getline( inStream, line );
include string and fstream.
Last edited by twomers; Dec 30th, 2008 at 1:16 pm.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Dec 31st, 2008
0

Re: Settings in Application (C++)

Click to Expand / Collapse  Quote originally posted by twomers ...
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:
C++ Syntax (Toggle Plain Text)
  1. std::cout<< "Hello, " << settings["USERNAME"] << ".";
Edit: I read it in like:
cpp Syntax (Toggle Plain Text)
  1. settings[line.substr( 0, colonPosition)] = line.substr(colonPosition+1);
where line is the result of reading the settings file via:
cpp Syntax (Toggle Plain Text)
  1. std::getline( inStream, line );
include string and fstream.
Well, to which file write ? Can a DLL be ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ronen444 is offline Offline
18 posts
since Dec 2008
Dec 31st, 2008
0

Re: Settings in Application (C++)

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:
cpp Syntax (Toggle Plain Text)
  1. bool readSettings( std::map<std::string,std::string> &settings, const std::string &fname ) {
  2. std::ifstream in( fname.cstr() );
  3. if ( !in )
  4. return false;
  5.  
  6. std::string line;
  7. size_t colonPosition;
  8. while ( std::getline( in, line ) ) {
  9. colonPosition = line.find( ":" );
  10. if ( colonPosition != std::string::npos ) // not found
  11. settings[line.substr( 0, colonPosition)] = line.substr(colonPosition+1);
  12. }
  13.  
  14. return true;
  15. }
(note, hasn't been tested. Might have to modify)
And the settings file would be like:
C++ Syntax (Toggle Plain Text)
  1. NAME:twomers
  2. AGE:22
Then I'd call it like:
cpp Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <string>
  3. #include <map>
  4. #include <iostream>
  5.  
  6. bool readSettings( std::map<std::string,std::string> &settings, const std::string &fname ) {
  7. std::ifstream in( fname.cstr() );
  8. if ( !in )
  9. return false;
  10.  
  11. std::string line;
  12. size_t colonPosition;
  13. while ( std::getline( in, line ) ) {
  14. colonPosition = line.find( ":" );
  15. if ( colonPosition != std::string::npos ) // not found
  16. settings[line.substr( 0, colonPosition)] = line.substr(colonPosition+1);
  17. }
  18.  
  19. return true;
  20. }
  21.  
  22. int main() {
  23. std::map<std::string,std::string> settings;
  24. readSettings( settings, "settings.txt" );
  25. std::cout<< settings["NAME"] << " is " << settings["AGE"] << " years old\n";
  26.  
  27. return 0;
  28. }
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.
Last edited by twomers; Dec 31st, 2008 at 10:43 am.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Dec 31st, 2008
0

Re: Settings in Application (C++)

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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 31st, 2008
0

Re: Settings in Application (C++)

>> 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.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: stdlib.h error?
Next Thread in C++ Forum Timeline: vector of strings error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC