Sign in to follow this  

Settings file

This topic is 3860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello again! Im looking for a way to save & load settings. Some developers are using a file full of commands...like this: settings.txt:

windowX=800

windowY=600

levelcount=4


Can someone tell me how to do it with c++? thanks.

Share this post


Link to post
Share on other sites
3Dgonewild,

It depends on your platform. If your using Windows then you have a couple of choices: using an .ini file or storing your settings in the registry. Using the .ini file would be easiest since you can modify it in your favourite text file viewer. Checkout the MSDN (if your VC++ EE then use the platform SDK docs) for:

GetPrivateProfileString()

This should give you all the infos needed.

Good Luck,

MAMEman.

Share this post


Link to post
Share on other sites
You could make your own text parser. You'll need to learn how to read from files, in this case, ifstream. Google that and you'll figure that out pretty quickly. Then set yourself a few rules to how your settings file will look. Can there be more than 1 property per line? does it use equal signs?

Once that is figured out, make your program read one line of your file at a time, and based on your rules, identify which property has what value.

That's the gist of it. If you want more clarity, just post so, but post your 'rules' as well so I can help you more specifically.

Share this post


Link to post
Share on other sites
@mameman:
My game is going to be multiplatform , so i cant use ini files.

@AdamGL:
Well , i know how to read one line , but thats all ..i have no idea how to make a text parser.

Here's a template of the settings file :

settings.txt

screenwidth=800;
screenheight=600;
totallevels=5;
hiscore_file=scores\score.txt;



Share this post


Link to post
Share on other sites
Quote:
Original post by 3Dgonewild
My game is going to be multiplatform , so i cant use ini files.


Your platform should have no effect on the usability of ini files.

Share this post


Link to post
Share on other sites
you don't really need a complex text parser for this as long as you keep your file format simple. If your settings file is nothing more than a list of key, value pairs(setting_name=value) then you can do this:

std::string setting_name;
std::string value;
while(fileStream)
{
std::getline(fileStream, setting_name, '=');
std::getline(fileStream, value);
if(setting_name == "windowX")
{
// Convert string to integer. This could be wrong, I haven't done it in awhile.
std::stringstream convert;
convert << value;
int value_int;
convert >> value_int;
// assign setting to proper variable
windowX = value_int
}
if(setting_name == "windowY")
{
// do stuff
}
// etc
}

Share this post


Link to post
Share on other sites
Quote:
Original post by nobodynews
you don't really need a complex text parser for this as long as you keep your file format simple. If your settings file is nothing more than a list of key, value pairs(setting_name=value) then you can do this:

*** Source Snippet Removed ***


Better yet, store all of the data inside a standard library map and extract the data you need from it:


#include <map>
#include <fstream>
#include <iostream>
#include <sstream>

int main(){
std::map<string, string> datafromfile;
std::ifstream infile("data.txt");
std::string key, data;
std::stringstream tempstream;

double damage;

infile >> key >> data;
while(infile){
datafromfile[key] = data;
infile >> key >> data;
}

if(datafromfile.find("WeaponDamage") == datafromfile.end){
std::cout << "WeaponDamage is not in the file! File invalid!" << std::endl;
}
else{
tempstream << datafromfile["WeaponDamage"];
tempstream >> damage;
}


}

Share this post


Link to post
Share on other sites
I've been looking at config file options, and I stumbled upon LibConfig. I don't know if it's any good, but it looks fairly promising (based on a 10 second skim at a quick look at the sample config file).

Share this post


Link to post
Share on other sites

This topic is 3860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this