| | |
Settings in Application (C++)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 18
Reputation:
Solved Threads: 0
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 ?
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: 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: Edit: I read it in like: where line is the result of reading the settings file via: include string and fstream.
•
•
•
•
USERNAME:twomers
USERAGE:22
C++ Syntax (Toggle Plain Text)
std::cout<< "Hello, " << settings["USERNAME"] << ".";
cpp Syntax (Toggle Plain Text)
settings[line.substr( 0, colonPosition)] = line.substr(colonPosition+1);
cpp Syntax (Toggle Plain Text)
std::getline( inStream, line );
Last edited by twomers; Dec 30th, 2008 at 1:16 pm.
•
•
Join Date: Dec 2008
Posts: 18
Reputation:
Solved Threads: 0
•
•
•
•
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:Edit: I read it in like:C++ Syntax (Toggle Plain Text)
std::cout<< "Hello, " << settings["USERNAME"] << ".";where line is the result of reading the settings file via:cpp Syntax (Toggle Plain Text)
settings[line.substr( 0, colonPosition)] = line.substr(colonPosition+1);include string and fstream.cpp Syntax (Toggle Plain Text)
std::getline( inStream, line );
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: (note, hasn't been tested. Might have to modify)
And the settings file would be like: Then I'd call it like: 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.
cpp Syntax (Toggle Plain Text)
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; }
And the settings file would be like:
C++ Syntax (Toggle Plain Text)
NAME:twomers AGE:22
cpp Syntax (Toggle Plain Text)
#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; }
Last edited by twomers; Dec 31st, 2008 at 10:43 am.
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
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.
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.
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.
![]() |
Similar Threads
- hijackthis.exe is not a valid win32 application (Viruses, Spyware and other Nasties)
- Having great difficulty with configuring IIS for ASP.Net application (ASP)
- How to backup Outlook Express email folders and settings (Windows NT / 2000 / XP)
- Quick Launch toolbar lost (Windows 95 / 98 / Me)
- Re: Hijack log-WMP Internal application error ha occured (Viruses, Spyware and other Nasties)
- 'c:\windows\explorer.exe is attempting to change internet protection settings'! (Viruses, Spyware and other Nasties)
- i have a dialer or something running in background.. (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: stdlib.h error?
- Next Thread: vector of strings error
| Thread Tools | Search this Thread |
api array beginner bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






