Page 1 of 1

sprintf issues

Posted: Sun Sep 10, 2017 9:42 am
by matteus
I gather there are problems with sprintf? I wondered what the alternative way to do this would be? strcpy?

Re: sprintf issues

Posted: Sun Sep 10, 2017 10:45 am
by Sik
Um, what? o_O Kind of hard to even discuss if you don't even say what's wrong with sprintf.

Also: all strcpy does is copy a string, while sprintf does a tad more (like converting numbers into strings, aligning text, etc.). Also sprintf is useful if you're concatenating more than two strings, or possibly even when it's just two (e.g. storing into a new string - let's just say that strcat is pretty crappy since it needs to scan all the original string before inserting the new one, no real advantage to doing strlen+strcpy and you may have taken the length from elsewhere already).

Re: sprintf issues

Posted: Sun Sep 10, 2017 12:14 pm
by matteus
Sik wrote:
Sun Sep 10, 2017 10:45 am
Um, what? o_O Kind of hard to even discuss if you don't even say what's wrong with sprintf.

Also: all strcpy does is copy a string, while sprintf does a tad more (like converting numbers into strings, aligning text, etc.). Also sprintf is useful if you're concatenating more than two strings, or possibly even when it's just two (e.g. storing into a new string - let's just say that strcat is pretty crappy since it needs to scan all the original string before inserting the new one, no real advantage to doing strlen+strcpy and you may have taken the length from elsewhere already).
Yeah I was doing all the conversion process before and then I learnt that sprintf was now supported. From what I gather though sprintf in the SGDK is buggy? Stef mentioned to me that it's not perfect.

Here are two examples where I use sprintf
// build string "You need X stakes"
strclr(MessageBoxLineStr1);
sprintf(MessageBoxLineStr1, "%s%d%s", YouNeedStr, StalksRequiredInt, Stakes1Str);
// build string "You have X stakes"
strclr(MessageBoxLineStr2);
sprintf(MessageBoxLineStr2, "%s%d%s", YouHaveStr, StakesInt, Stakes1Str);
I'm just unsure what my alternative is given strcat is crappy :)

Re: sprintf issues

Posted: Sun Sep 10, 2017 2:01 pm
by Stef
myself i'm using mix of strcpy(..), strcat(..) and intToStr(..), but it's a pain compared to sprintf(..)
Also if sprintf(..) is working for you keep it of course, it' just that myself i already got some problems with it but very rarely.

Re: sprintf issues

Posted: Sun Sep 10, 2017 2:47 pm
by matteus
Thanks for dropping a reply Stef! What kind of problems does it cause? I'm just wary of doing anything that will result in crashes on real hardware :)

Re: sprintf issues

Posted: Sun Sep 10, 2017 10:16 pm
by Stef
When the function fails, it also fails on emulator... doesn't crash, just does not display correctly values as far i remember ;)