![]() |
| ||
| Settings in Application (C++) 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 ? |
| ||
| 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:
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. |
| ||
| Re: Settings in Application (C++) Quote:
|
| ||
| 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: bool readSettings( std::map<std::string,std::string> &settings, const std::string &fname ) {(note, hasn't been tested. Might have to modify)And the settings file would be like: NAME:twomersThen I'd call it like: #include <fstream>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. |
| ||
| 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. |
| ||
| 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. |
| All times are GMT -4. The time now is 12:55 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC