|
Write INT to config file |
MacGyver1979
Member #14,135
March 2012
|
Hello together, i have a question regarding the configfile. I think there are two solutions for it, Maybe anyone can explain me the "right" way with an short example? With best regards MacGyver |
William Labbett
Member #4,486
March 2004
|
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 }
|
gnolam
Member #2,030
March 2002
|
MacGyver1979
Member #14,135
March 2012
|
My solution 1const char* convertInt(int number)
2{
3 if (number == 0)
4 return "0";
5 string temp="";
6 string returnvalue="";
7 while (number>0)
8 {
9 temp+=number%10+48;
10 number/=10;
11 }
12 for (int i=0;i<temp.length();i++)
13 returnvalue+=temp[temp.length()-i-1];
14
15 const char *c = returnvalue.c_str();
16 return c;
17}
Is there a better solution for it? |
Arthur Kalliokoski
Second in Command
February 2005
|
Click on the links gnolam posted. They all watch too much MSNBC... they get ideas. |
MacGyver1979
Member #14,135
March 2012
|
Hello Arthur Kalliokoski, and what's wrong with my posted solution? Greats MacGyver |
gnolam
Member #2,030
March 2002
|
For a C++ solution, use stringstreams. [EDIT] MacGyver1979 said: and what's wrong with my posted solution? Two major things from a quick skim:
-- |
Arthur Kalliokoski
Second in Command
February 2005
|
MacGyver1979 said: 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. They all watch too much MSNBC... they get ideas. |
MacGyver1979
Member #14,135
March 2012
|
Hello, thanks for that quick response. Won't handle negative integers Negative integers are not given, but i understand you. I'm not so confirm with snprintf() can anyone explane me with a little example? Greats MacGyver |
LennyLen
Member #5,313
December 2004
|
MacGyver1979 said: 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("");
|
MacGyver1979
Member #14,135
March 2012
|
Ok works for me. 1#include <sstream>
2
3 stringstream str;
4 str << 50;
5 const char *c = str.str().c_str();
|
William Labbett
Member #4,486
March 2004
|
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. I should have posted what Gnolam posted. 1int write_int_to_config(ALLEGRO_CONFIG *cp, const char *section, const char *key, int i)
2{
3 char s[30];
4
5
6 if( snprintf( s, 30, "%d", i) == 0)
7 {
8 return 1; //error
9 }
10 else
11 {
12 al_set_config_value( cp, section, key, (const char *) s);
13 return 0; //okay
14 }
15}
|
Arthur Kalliokoski
Second in Command
February 2005
|
William Labbett said: you find yourself doing things the hard way due to lack of experience. They all watch too much MSNBC... they get ideas. |
William Labbett
Member #4,486
March 2004
|
|
|