sprintf issues

SGDK only sub forum

Moderator: Stef

Post Reply
matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

sprintf issues

Post by matteus » Sun Sep 10, 2017 9:42 am

I gather there are problems with sprintf? I wondered what the alternative way to do this would be? strcpy?

Sik
Very interested
Posts: 939
Joined: Thu Apr 10, 2008 3:03 pm
Contact:

Re: sprintf issues

Post by Sik » 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).
Sik is pronounced as "seek", not as "sick".

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: sprintf issues

Post by matteus » Sun Sep 10, 2017 12:14 pm

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 :)
Last edited by matteus on Sun Sep 10, 2017 2:48 pm, edited 1 time in total.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: sprintf issues

Post by Stef » Sun Sep 10, 2017 2:01 pm

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.

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: sprintf issues

Post by matteus » Sun Sep 10, 2017 2:47 pm

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

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: sprintf issues

Post by Stef » Sun Sep 10, 2017 10:16 pm

When the function fails, it also fails on emulator... doesn't crash, just does not display correctly values as far i remember ;)

Post Reply