Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Write INT to config file

This thread is locked; no one can reply to it. rss feed Print
Write INT to config file
MacGyver1979
Member #14,135
March 2012

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

William Labbett
Member #4,486
March 2004
avatar

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
avatar

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

MacGyver1979
Member #14,135
March 2012

My solution

#SelectExpand
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
avatar

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
avatar

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.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Arthur Kalliokoski
Second in Command
February 2005
avatar

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
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

LennyLen
Member #5,313
December 2004
avatar

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.
Thanks

#SelectExpand
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
avatar

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.

#SelectExpand
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
avatar

you find yourself doing things the hard way due to lack of experience.

Been there, done that.

They all watch too much MSNBC... they get ideas.

William Labbett
Member #4,486
March 2004
avatar

:)

Go to: