Welcome, Guest. Please login or register.

Author Topic: printf output to string possible ?  (Read 3148 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show only replies by Jose
printf output to string possible ?
« on: September 10, 2005, 12:05:55 PM »
Hi. Is there a way that one can have the output of printf() put into a string instead of stdout ? I know there's a function to change the output stream but it requires a file or handle to be passed IIRC...
I have done a function that does this but it's a bit limited on the types it uses, I thought that maybe printf() could be used for this also....
\\"We made Amiga, they {bleep}ed it up\\"
 

Online Thomas

Re: printf output to string possible ?
« Reply #1 on: September 10, 2005, 12:09:14 PM »

What about sprintf ?

sprintf (string,format,data);

Bye,
Thomas

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show only replies by Jose
Re: printf output to string possible ?
« Reply #2 on: September 10, 2005, 12:28:53 PM »
@Thomas

Sounds great :-)
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: printf output to string possible ?
« Reply #3 on: September 10, 2005, 12:44:32 PM »
Indeed sprintf is your friend. However sometimes you want to avoid linking whole stdio just for that. You might then want to use exec/RawDoFmt:

Code: [Select]

#include <proto/exec.h>
#include <proto/dos.h>

static const
UWORD stuffChar[] = {0x16c0, 0x4e75};

int main(void)
{
  UBYTE string[256];
  LONG array[2];

  array[0] = (LONG) &quot;Fish&quot;;
  array[1] = 2;

  RawDoFmt(&quot;%s have %ld eyes.&quot;, array, (void (*)(void)) &stuffChar, string);

  PutStr(string);
  PutStr(&quot;\n&quot;);

  return 0;
}


However, please note that the default width of the %d, %u, %x and %c is 16-bit, not 32-bit as with printf functions. So you must use %ld, %lu, %lx and %lc if your array is LONGs. RawDoFmt is also limited in some other ways compared to sprintf. For simple stuff RawDoFmt is fine, however.
 

Offline CrazyProg

  • Newbie
  • *
  • Join Date: Oct 2004
  • Posts: 14
    • Show only replies by CrazyProg
Re: printf output to string possible ?
« Reply #4 on: September 10, 2005, 01:18:36 PM »
Hi,

Instead of using 'sprintf' it is better to use 'snprintf'.  You specify how big the 'string' is so there isn't any chance of a buffer overrun crashing your program. :-)

If you are worried abount code size you could use the version in the newlib library.

Regards Neil.
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show only replies by Jose
Re: printf output to string possible ?
« Reply #5 on: September 10, 2005, 02:49:35 PM »
@Piru
"(void (*)(void))"

Wouldn't a simple cast to void work here? I don't get it, it's not even a function.. :-?

@CrazyProg

Yeah that's cool, but I'm allocating the space dynamically after checking the size with strlen().

\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: printf output to string possible ?
« Reply #6 on: September 10, 2005, 03:05:35 PM »
Quote
Wouldn't a simple cast to void work here? I don't get it, it's not even a function..

It isn't a function, but I want the compiler to believe it is (to avoid some warning).

Code: [Select]
static const
UWORD stuffChar[] = {0x16c0, 0x4e75};

Is really:
Code: [Select]

_stuffChar:
  move.b d0,(a3)+
  rts

It's much easier to use direct hexdump of the instructions than to try to get the compiler to produce something as simple.
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show only replies by Jose
Re: printf output to string possible ?
« Reply #7 on: September 11, 2005, 11:05:34 AM »
Ahhh... ok.  :-)
\\"We made Amiga, they {bleep}ed it up\\"