Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Creating and saving config files

Credits go to Edgar Reynaldo and SiegeLord for helping out!
This thread is locked; no one can reply to it. rss feed Print
Creating and saving config files
bill99
Member #11,913
May 2010

Ive managed to create and save a config file, worked fine except at first the very first value saved was all messed up. it was some weird symbols I've never seen before (along with a very tiny "L"). Now the first and second ones are messed up, here are some values (taken from the saved config file), they are different each time i run the program. (they are supposed to be numbers)

Ok apparently i cant even post the messed up values cause it causes an xml parsing error when i hit the post new thread button. But trust me they are messed up.

Ok the last one the second value came out ok. What i cant figure out is why this is happening... any ideas?

SiegeLord
Member #7,827
October 2006
avatar

How are you calling al_set_config_value? How are you converting the numbers to strings?

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

bill99
Member #11,913
May 2010

Here is my function for converting numbers to strings:

#SelectExpand
1const char * IntToString(int T) 2 { 3 const char * Value; 4 std::string Temp; 5 std::stringstream ss; 6 ss << T; 7 Temp = ss.str(); 8 Value = Temp.c_str(); 9 return Value; 10 }

and here is my call to al_set_config_value

#SelectExpand
1void FileTest::Save(ALLEGRO_CONFIG * Config) 2{ 3 al_set_config_value(Config, "Values", "One", IntToString(One)); 4 al_set_config_value(Config, "Values", "Two", IntToString(Two)); 5 al_set_config_value(Config, "Values", "Three", IntToString(Three)); 6 al_set_config_value(Config, "Names", "Name", Name.c_str()); 7 al_set_config_value(Config, "Names", "Other", Other.c_str()); 8 al_set_config_value(Config, "Names", "Stuff", Stuff.c_str()); 9 10}

this function is part of a class(obviously) i made just to test the functionality of the ini files as im new to it.

Edgar Reynaldo
Member #8,592
May 2007
avatar

You're returning the stored const char* .c_str() from a temporary object, namely Temp, your string. When the function exits Temp goes out of scope, just like its name says it does and then the reference you are holding is no longer valid. Return a copy of a std::string instead and then .c_str() it later when you need to.

//ex
 al_set_config_value(Config, "Values", "One", IntToString(One).c_str());

bill99
Member #11,913
May 2010

That did it! Thank you both for your help! I really appreciate it. ;D

Go to: