Settings in Application (C++)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 18
Reputation: Ronen444 is an unknown quantity at this point 
Solved Threads: 0
Ronen444 Ronen444 is offline Offline
Newbie Poster

Settings in Application (C++)

 
0
  #1
Dec 30th, 2008
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 ?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,858
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Settings in Application (C++)

 
0
  #2
Dec 30th, 2008
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:
  1. std::cout<< "Hello, " << settings["USERNAME"] << ".";
Edit: I read it in like:
  1. settings[line.substr( 0, colonPosition)] = line.substr(colonPosition+1);
where line is the result of reading the settings file via:
  1. std::getline( inStream, line );
include string and fstream.
Last edited by twomers; Dec 30th, 2008 at 1:16 pm.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: Ronen444 is an unknown quantity at this point 
Solved Threads: 0
Ronen444 Ronen444 is offline Offline
Newbie Poster

Re: Settings in Application (C++)

 
0
  #3
Dec 31st, 2008
Originally Posted by twomers View Post
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:
  1. std::cout<< "Hello, " << settings["USERNAME"] << ".";
Edit: I read it in like:
  1. settings[line.substr( 0, colonPosition)] = line.substr(colonPosition+1);
where line is the result of reading the settings file via:
  1. std::getline( inStream, line );
include string and fstream.
Well, to which file write ? Can a DLL be ?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,858
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Settings in Application (C++)

 
0
  #4
Dec 31st, 2008
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:
  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:
  1. NAME:twomers
  2. AGE:22
Then I'd call it like:
  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.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Settings in Application (C++)

 
0
  #5
Dec 31st, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,858
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Settings in Application (C++)

 
0
  #6
Dec 31st, 2008
>> 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.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC