Creating and saving config files

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?


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


Here is my function for converting numbers to strings:

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

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

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.

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


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

Thread #615130. Printed from