Hello together,
i have a question regarding the configfile.
I want to write some INT values in it, but the value should be a const char*.
I'm working with ALLEGRO 5.0.4.
I think there are two solutions for it,
1. convert int to const char* (not knowing)
2. there is a function to write a int value to the config file (not knowing)
Maybe anyone can explain me the "right" way with an short example?
With best regards
MacGyver
Write yourself a function
int write_int_to_config(int i) { char s[30]; // convert i to string - that's a tricky job but not too hard. // use al_write_to_config( (const char *)s, .... ) or whatever the function is }
My solution
Is there a better solution for it?
Click on the links gnolam posted.
Hello Arthur Kalliokoski,
and what's wrong with my posted solution?
Greats MacGyver
For a C++ solution, use stringstreams.
[EDIT]
and what's wrong with my posted solution?
Two major things from a quick skim:
Won't handle negative integers
returnvalue goes out of scope, so the pointer that .c_str() returns becomes invalid as soon as that function returns.
and what's wrong with my posted solution?
I didn't check it at all, but reinventing the wheel is usually a waste of time and often leads to bugs.
Hello,
thanks for that quick response.
Won't handle negative integers
returnvalue goes out of scope, so the pointer that .c_str() returns becomes invalid as soon as that function returns.
Negative integers are not given, but i understand you.
I'm not so confirm with
snprintf()
strtol()
can anyone explane me with a little example?
Greats MacGyver
can anyone explane me with a little example?
Those are C functions. Since you are using C++, you should use stringstreams as gnolam said.
something like:
#include <sstrream> ... stringstream str; str << int_value_I_want_to_convert; al_set_config_value(config_file, section, key, (str.str()).c_str()); str.str("");
Ok works for me.
Thanks
Part of being a newb (I know this from experience) is that you find yourself doing things the hard way due to lack of experience. Then you're reluctant to do things the easy way because it means all your previous hard work was a waste of time.
Anyway, here's the easy way :
I should have posted what Gnolam posted.
you find yourself doing things the hard way due to lack of experience.