Write INT to config file
MacGyver1979

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

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
MacGyver1979

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

Click on the links gnolam posted.

MacGyver1979

Hello Arthur Kalliokoski,

and what's wrong with my posted solution?

Greats MacGyver

gnolam

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.

Arthur Kalliokoski

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.

MacGyver1979

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

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

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

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

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

Been there, done that.

William Labbett

:)

Thread #609795. Printed from Allegro.cc